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.

434
281
247
933
899
60
602
569
501
22
447
137
161
363
666
636
172
212
636
331
478
452
428
736
945
115
667
470
691
665
397
587
511
853
727
261
484
305
810
593
713
967
791
199
164
825
413
394
398
493
759
803
321
863
3
660
664
96
561
24
448
953
89
307
595
51
757
166
117
233
94
326
641
882
218
671
59
644
266
114
110
436
66
55
226
762
282
611
659
888
908
295
412
732
77
124
857
884
412
613
BubbleSort - 0 steps
487
646
125
569
554
787
336
369
126
585
176
333
687
955
147
792
462
609
493
530
141
21
823
557
148
712
345
109
355
384
643
1000
778
180
73
160
797
517
907
82
959
176
769
234
294
70
151
937
665
2
493
677
923
842
882
218
269
853
727
502
979
867
95
838
433
692
434
488
748
110
781
17
983
802
336
557
878
158
39
147
54
701
224
304
397
570
960
382
57
975
866
813
766
667
846
554
120
351
424
374
InsertionSort - 0 steps
500
164
231
726
721
166
979
264
292
793
875
296
6
546
193
663
26
375
566
325
890
233
818
422
165
484
315
622
678
441
620
665
912
720
393
15
887
651
157
184
245
697
820
502
107
847
352
32
669
908
230
65
167
226
895
437
51
377
838
795
173
952
104
977
403
89
337
603
84
274
356
751
112
888
696
194
610
253
344
648
527
279
914
389
648
743
16
999
729
781
156
192
74
907
288
635
768
393
105
568
ShellSort - 0 steps
579
88
268
269
131
269
799
748
817
97
555
810
802
526
66
770
337
492
557
963
89
219
278
247
313
619
588
30
355
832
845
57
555
803
569
756
200
10
493
275
597
300
464
961
277
374
618
305
476
245
215
316
188
372
882
972
9
966
963
709
492
561
244
424
279
463
264
887
491
874
218
797
17
831
911
51
739
152
162
17
67
860
308
153
134
604
415
719
143
428
584
93
549
697
911
14
422
567
917
970
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