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.

316
814
23
182
534
371
591
765
361
653
808
527
510
563
158
681
311
436
593
946
305
935
573
323
482
221
553
289
938
946
519
359
858
229
422
31
51
267
643
226
836
977
758
571
7
910
665
258
34
64
147
672
34
682
492
827
681
876
560
352
212
566
133
361
546
739
494
574
698
529
43
847
841
48
215
432
730
981
678
107
257
830
385
310
114
973
924
852
43
880
663
307
927
583
819
494
746
89
255
355
BubbleSort - 0 steps
631
923
743
738
124
115
896
549
790
459
697
112
416
498
911
247
382
453
944
896
899
726
740
268
876
54
438
139
941
988
923
889
686
559
847
195
197
460
510
861
549
928
400
890
691
801
308
698
276
646
593
817
756
471
871
700
577
645
576
660
909
995
630
934
389
499
815
138
71
928
218
449
40
124
938
88
170
821
620
184
312
311
154
595
767
714
695
171
928
603
65
728
189
421
993
28
798
13
410
173
InsertionSort - 0 steps
115
319
768
259
377
61
527
954
268
13
17
625
766
284
579
598
482
662
84
170
52
953
323
763
560
718
865
178
653
19
21
826
444
326
720
80
176
607
820
832
543
31
582
512
13
432
390
757
28
16
835
132
590
762
681
545
401
319
221
994
686
596
982
840
500
420
491
415
802
313
107
921
980
325
169
371
442
360
469
822
130
94
222
235
181
650
163
595
373
262
362
822
908
670
207
903
530
144
811
17
ShellSort - 0 steps
903
554
69
273
776
714
39
633
545
787
931
201
427
132
327
361
834
976
391
446
96
465
871
189
67
798
423
865
91
200
720
763
616
652
515
368
975
833
541
554
396
991
131
408
208
33
76
600
752
400
363
622
505
262
798
39
719
625
654
907
446
705
195
637
323
31
612
156
89
872
254
989
194
948
371
763
807
637
887
494
620
709
147
654
118
498
976
98
718
590
938
343
363
778
804
628
940
943
731
278
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