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.

729
519
668
879
140
790
617
145
562
843
550
347
495
251
788
939
804
978
677
477
894
261
860
40
406
207
847
525
636
549
524
571
460
824
209
643
31
646
588
918
24
106
75
874
391
989
671
191
3
920
899
331
21
107
828
655
377
624
972
964
586
650
883
91
547
453
159
515
210
225
153
889
711
689
300
66
620
245
429
400
883
177
907
996
456
296
670
708
900
818
877
253
283
680
469
338
817
705
323
206
BubbleSort - 0 steps
674
31
976
275
758
399
49
489
475
236
796
501
260
880
447
320
928
729
782
891
336
570
501
893
22
143
347
897
682
148
1
643
917
946
134
886
126
281
856
207
650
326
72
458
370
122
221
893
869
285
162
250
533
568
187
592
651
725
540
319
542
923
985
283
805
316
384
433
597
144
564
35
938
700
800
340
708
693
672
1000
610
6
533
9
847
901
814
684
163
62
151
834
24
227
629
682
221
322
679
714
InsertionSort - 0 steps
494
579
540
483
124
4
348
133
183
999
480
365
914
832
952
179
830
508
612
687
352
338
668
998
453
741
37
208
128
840
854
538
665
130
222
284
212
651
143
343
428
95
899
488
668
92
99
225
361
275
569
431
590
443
338
909
668
350
397
6
210
147
957
712
86
743
623
918
859
109
35
723
632
995
349
155
165
160
231
502
81
186
833
950
94
836
614
562
378
180
415
43
537
90
290
747
27
867
237
543
ShellSort - 0 steps
666
542
420
549
172
885
830
687
800
453
303
480
803
812
466
125
163
180
752
663
824
471
916
516
550
666
789
333
152
33
30
749
727
925
25
452
547
863
554
74
76
368
929
94
699
679
56
235
765
794
117
331
102
561
408
524
163
603
28
244
258
453
165
861
980
520
806
734
707
242
656
649
185
591
759
211
922
234
140
699
268
91
285
732
334
349
821
498
139
17
765
148
579
520
459
936
256
177
784
952
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