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.

518
192
576
438
752
662
352
765
927
374
776
507
983
714
973
298
807
548
85
414
164
1
665
137
797
792
106
735
635
359
965
243
957
599
905
216
980
184
527
60
733
461
609
335
749
876
153
860
854
363
27
498
465
580
653
969
472
125
548
802
331
154
91
196
985
280
420
668
80
658
852
318
744
623
582
820
996
977
902
846
119
748
516
390
114
58
127
1
386
116
822
569
777
501
423
998
19
660
364
879
BubbleSort - 0 steps
384
111
434
514
753
648
383
509
760
819
506
880
668
202
805
455
596
691
882
422
574
333
185
638
396
247
703
552
421
699
912
532
266
785
134
657
729
305
271
362
989
322
734
683
772
469
257
373
698
669
185
11
735
625
976
38
92
817
227
417
444
199
274
857
505
684
674
153
714
257
883
799
346
627
822
933
161
435
524
428
976
535
774
465
705
436
755
901
494
890
532
187
645
387
907
170
320
63
649
707
InsertionSort - 0 steps
560
974
260
405
615
457
239
351
940
514
633
113
117
181
975
406
784
543
53
921
404
918
84
886
420
618
901
384
411
347
763
54
284
760
818
719
962
160
461
360
682
244
92
149
744
118
157
619
219
458
564
396
218
386
765
57
816
907
44
235
721
702
914
571
593
464
751
729
991
588
401
57
250
209
110
344
236
446
450
117
714
766
910
608
485
604
340
679
943
600
586
759
843
949
872
194
239
504
632
603
ShellSort - 0 steps
234
641
833
462
163
711
236
842
29
671
946
810
554
427
1
47
232
705
125
292
433
311
809
818
680
425
739
59
414
938
656
546
819
487
189
638
593
214
731
505
958
49
541
485
638
489
137
313
320
131
562
621
729
990
58
533
353
447
573
423
75
727
701
253
353
570
382
655
155
436
791
654
795
26
187
548
386
731
956
901
706
19
972
1
799
843
515
632
958
189
253
736
250
91
270
304
440
812
432
710
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