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.

792
510
707
271
663
304
241
739
507
550
151
82
383
681
432
696
378
1
407
715
509
762
217
648
32
679
817
709
480
771
600
636
967
988
836
2
485
88
82
249
189
396
672
910
344
765
228
878
813
186
540
728
508
12
404
679
608
183
333
462
791
247
508
942
228
907
4
259
688
936
668
121
502
291
153
247
24
740
929
230
127
49
652
176
404
45
513
43
793
71
31
897
416
938
249
93
731
996
192
497
BubbleSort - 0 steps
181
449
193
550
631
28
470
975
892
169
530
539
588
21
168
863
788
513
520
927
304
584
248
943
919
31
941
786
249
973
535
915
306
490
736
912
827
285
637
86
971
645
132
116
78
835
272
326
80
58
246
879
52
238
183
180
473
512
670
524
398
629
74
394
506
555
66
52
700
28
799
497
957
468
82
847
939
388
92
492
153
857
921
165
187
637
982
158
252
475
677
26
974
909
83
931
594
647
664
738
InsertionSort - 0 steps
624
269
756
726
974
281
947
812
70
502
504
199
839
296
969
117
55
776
116
332
393
106
669
375
49
99
84
261
111
281
151
72
459
269
661
395
412
123
475
353
373
584
222
8
661
509
472
763
211
304
237
779
632
406
299
57
766
118
654
612
802
168
345
780
166
474
247
578
58
460
702
763
871
643
675
738
69
826
418
203
412
197
469
102
438
691
866
474
209
224
251
384
594
167
687
382
508
466
31
902
ShellSort - 0 steps
367
111
94
303
473
266
552
713
724
761
210
244
880
583
643
793
367
155
989
715
315
75
895
834
994
789
983
525
842
501
943
843
352
250
187
668
462
424
444
297
601
250
764
595
900
978
230
556
52
307
135
814
179
651
934
680
618
733
808
16
290
511
144
583
731
288
143
702
536
375
851
71
578
282
123
987
403
648
856
124
998
572
441
173
733
18
635
196
733
369
389
790
604
856
570
879
524
24
206
408
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