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.

498
720
526
968
577
138
577
258
521
274
272
596
98
349
91
93
664
283
53
659
845
523
857
815
13
134
312
876
542
890
539
741
329
362
398
931
243
333
262
491
406
519
790
518
840
405
38
281
367
61
972
970
184
879
910
449
678
899
622
225
345
834
616
672
138
992
894
442
96
865
728
288
930
996
540
889
75
83
205
238
192
729
661
81
769
608
890
781
266
810
194
60
186
381
982
523
183
375
6
997
BubbleSort - 0 steps
648
452
38
870
560
818
917
178
993
701
482
170
138
823
222
2
634
693
691
675
397
415
751
196
890
913
239
986
29
896
430
855
569
462
454
849
417
387
583
986
220
853
961
859
974
761
747
908
886
663
439
334
67
410
464
680
788
658
162
657
739
715
402
496
683
863
722
803
91
398
933
507
63
301
238
809
90
998
838
810
815
920
805
628
302
535
181
231
270
722
22
815
228
542
123
55
366
84
929
391
InsertionSort - 0 steps
915
69
191
461
190
503
499
344
984
163
122
669
61
904
939
358
723
656
172
979
807
507
984
453
735
539
375
527
719
853
114
977
588
170
954
478
930
158
629
466
941
596
406
806
306
393
349
962
751
841
648
270
940
240
685
841
568
355
700
67
543
81
486
997
819
826
841
926
564
805
760
18
889
837
109
293
94
646
238
336
705
492
590
330
505
622
941
183
329
4
806
636
998
806
457
940
705
497
716
834
ShellSort - 0 steps
301
674
638
897
663
873
784
202
552
283
305
835
205
400
170
930
905
509
920
221
157
328
186
740
117
653
103
43
167
807
952
754
977
172
765
96
99
838
944
733
593
679
566
851
714
303
816
433
455
135
666
375
311
795
162
330
411
770
794
930
664
36
583
844
150
965
872
625
78
608
911
534
593
245
455
813
818
282
625
641
205
306
594
553
591
145
420
942
447
14
405
295
832
226
582
783
542
842
177
446
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