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.

418
879
659
537
77
686
578
92
136
446
147
694
470
642
908
825
508
497
490
371
185
832
205
968
270
220
514
8
467
180
428
682
837
155
75
836
841
162
597
294
769
948
771
105
295
310
754
601
709
15
823
732
253
522
777
528
224
573
136
451
908
663
856
114
891
316
956
125
461
825
348
938
365
295
372
33
585
650
13
167
543
146
66
455
22
45
284
808
549
821
887
692
319
834
905
704
13
555
321
103
BubbleSort - 0 steps
166
833
108
244
285
518
360
522
388
146
996
739
601
694
884
399
485
485
234
776
737
929
6
24
805
200
882
766
749
204
381
714
182
86
453
42
818
663
813
55
322
20
717
294
960
896
572
418
55
692
461
192
795
955
257
314
656
314
152
608
944
859
711
914
184
589
52
61
392
451
297
887
913
319
539
775
299
219
970
138
367
286
726
150
727
424
249
547
343
295
942
506
233
713
112
20
288
940
829
881
InsertionSort - 0 steps
324
660
953
881
656
946
878
928
838
521
915
657
871
753
425
540
602
860
145
257
795
268
704
120
602
7
897
794
334
75
715
795
620
176
988
105
807
697
28
220
380
669
895
673
301
147
587
824
996
514
671
65
221
930
759
774
122
799
41
38
652
114
773
370
290
384
507
276
536
199
951
365
974
511
770
305
955
80
715
333
987
675
137
457
788
859
935
759
531
514
759
304
583
648
256
309
980
157
292
821
ShellSort - 0 steps
777
419
116
686
945
101
466
614
379
821
96
801
343
218
46
80
120
544
942
918
590
116
733
51
539
25
154
207
579
490
583
133
452
822
382
336
358
646
543
266
253
841
379
373
456
233
443
100
854
72
433
753
26
613
532
668
582
227
679
661
227
598
655
667
251
237
375
918
602
528
854
467
189
301
292
441
101
308
693
166
741
785
103
311
754
481
337
496
627
336
525
507
408
246
724
173
536
339
233
651
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