skip to content

JavaScript: Sorting Algorithm Comparison

In this article we present a visualizaion of four different JavaScript DHTML sorting classes all of which have been described in more detail in previous articles.

Sorting Algorithm Visualization

Below you will see four scrambled versions of the same image. When you use the controls below to 'solve' the puzzles they will each use a different sorting algorithm as indicated - Bubble, Insertion, Shell and Quick Sort - to rearrange the pieces.

You can watch in real time as the sorting takes place and see an updating counter of the number of steps taken so far - where a 'step' is the process of exchanging two puzzle pieces.

123
821
545
364
906
652
936
667
52
420
202
130
21
377
262
388
446
72
738
319
487
890
17
260
357
637
782
311
534
877
304
673
858
194
443
294
746
257
668
808
428
534
66
879
627
694
254
116
443
616
330
962
926
801
717
100
12
238
286
961
591
710
43
978
724
474
399
690
335
351
920
751
32
119
249
402
470
99
308
915
745
586
66
835
107
72
473
340
614
820
563
522
611
347
341
235
382
125
183
287
BubbleSort - 0 steps
4
607
589
67
213
254
965
327
187
492
573
551
186
720
964
553
816
240
289
157
835
448
420
913
455
210
391
305
240
455
17
419
582
531
467
820
994
921
540
861
79
992
141
240
159
405
955
507
225
713
117
698
547
59
186
972
882
439
136
450
909
565
267
189
103
904
945
280
841
774
388
224
746
808
492
622
391
874
522
639
487
494
797
167
606
102
577
1000
791
159
558
524
318
847
893
59
959
803
975
690
InsertionSort - 0 steps
405
114
64
925
997
333
117
864
375
119
132
30
473
778
452
163
451
722
120
298
433
268
782
633
298
773
132
954
52
592
786
32
263
569
66
495
31
961
423
190
390
571
862
94
893
900
995
749
63
401
910
659
215
814
76
729
275
802
608
496
921
409
70
40
699
109
728
612
679
757
449
646
539
73
432
210
820
218
497
731
897
628
932
876
879
376
179
104
920
849
446
890
680
738
924
935
383
354
573
867
ShellSort - 0 steps
912
661
225
930
113
811
424
627
998
761
662
437
995
864
718
265
239
137
475
812
243
691
924
501
433
8
82
477
97
862
153
837
544
983
997
391
617
547
958
138
664
243
707
665
730
494
386
710
511
115
999
57
314
704
747
677
764
154
986
726
594
125
347
442
78
267
110
481
322
684
446
709
482
766
911
584
628
360
601
691
589
370
979
166
975
864
5
860
484
748
510
432
710
894
165
496
135
673
107
31
QuickSort - 0 steps
Controls 1) Select an image; 2) Click 'SOLVE'. * images generated by Stable Diffusion and Midjourney

All of the sorting is powered by JavaScript in your web browser so there is no load at all on the web server. There is also only a single background image being used each time - they haven't been sliced up into smaller squares for the puzzle.

While there are other methods for shuffling and sorting values, the advantage of DHTML sorting - rearranging actual HTML elements within the DOM - is that it preserves any event handlers or other dynamically assigned properties that may have been assigned to the elements.

This is possible because we are working with a 'live' NodeList which means that "changes in the DOM automatically update the collection."

Comparison of Results

As expected, the Bubble Sort and Insertion Sort algorithms are relatively slow requiring a large number of steps to solve the puzzle. This is mainly down to the fact that they can only swap adjacent squares.

The Insertion Sort and Quick Sort algorithms are significantly faster thanks to their more advanced algorithms requiring only a fraction of the number of steps each time to reconfigure the puzzle pieces.

We generally use the Shell Sort algorithm which, despite being slightly slower, is a stable sort, whereas Quick Sort is unstable (a sorting algorithm is said to be stable "when two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array").

What do we use if for?

Apart from these fascinating visualizations we typically use JavaScript DHTML sorting when presenting tabular data. It allows us to have the table contents sorted by various values on demand without needing to re-request data from the web server.

You can see some examples of this in earlier articles on the subject. The code used here for the visualization has been adapted slightly to insert a delay, but is otherwise identical to the code presented there.

We were able to insert delays into the sorting process by converting the exchange step to use a generator function which is then called repeatedly by setInterval. Generators have the effect of allowing you to 'pause' and 'resume' execution within a function.

Another interesting use case would be maintaining a 'pole position' graphic where race data was being dynamically inserted into the page and the task was to keep the list in the right order - perhaps with a touch of animation.

If you find a use for this code in your website or project please let us know using the comments button below.

< JavaScript

Post your comment or question
top