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.

648
878
897
768
551
169
502
133
707
793
525
93
673
456
591
516
998
942
980
989
570
933
702
3
738
778
866
993
10
142
143
576
125
475
996
341
169
911
238
384
680
973
177
472
777
429
516
388
920
89
48
731
995
970
15
486
869
968
149
308
113
391
738
369
469
104
170
183
439
572
906
556
179
94
900
764
939
630
133
685
735
186
274
199
598
649
935
271
615
681
984
876
697
629
409
863
938
296
399
12
BubbleSort - 0 steps
729
907
720
616
998
1000
271
68
706
637
726
859
590
545
484
363
128
577
608
671
979
224
74
858
198
427
986
88
150
921
606
86
482
802
45
877
188
802
173
400
414
950
108
318
49
984
320
709
348
251
998
489
698
949
359
283
258
853
293
215
716
932
722
141
174
553
899
356
444
374
940
938
854
267
2
752
258
236
963
162
86
684
837
96
187
29
724
72
729
467
509
601
673
453
964
308
970
269
338
922
InsertionSort - 0 steps
783
458
512
371
744
775
108
600
667
675
277
720
1000
894
141
865
614
478
657
470
784
589
257
997
983
484
933
113
597
667
745
45
904
661
236
624
1
632
371
13
702
323
530
295
243
845
275
413
617
260
117
490
638
1
514
362
48
692
472
554
918
368
710
777
905
364
155
83
894
331
184
436
147
944
638
337
496
771
465
818
481
219
841
497
413
405
590
447
691
290
40
152
999
671
733
34
951
89
21
693
ShellSort - 0 steps
862
448
414
823
553
397
854
403
571
280
396
365
956
888
914
53
145
673
38
536
424
541
307
897
21
364
971
680
359
687
768
332
890
365
377
371
63
674
636
365
807
517
871
108
341
218
751
987
31
273
368
162
854
806
627
503
470
363
776
516
862
656
825
168
525
817
658
374
331
361
132
1000
829
819
665
945
524
103
856
291
445
132
233
666
674
78
439
119
480
934
62
318
24
318
708
800
788
21
178
372
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