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.

127
4
620
143
832
556
419
657
22
713
741
658
318
966
619
634
348
282
553
938
880
174
454
962
181
426
182
393
537
168
226
32
140
969
881
805
43
912
273
1
417
950
722
115
887
370
992
847
297
149
228
535
694
450
89
1
842
180
347
214
93
79
448
192
6
76
4
132
141
675
884
198
621
320
564
564
207
252
109
399
632
369
958
520
419
665
351
200
286
94
521
907
305
925
46
305
972
776
156
577
BubbleSort - 0 steps
992
343
753
504
790
795
175
781
557
71
74
179
950
349
558
777
865
270
366
737
947
527
786
15
826
259
42
776
837
472
602
15
513
169
337
354
266
562
195
742
121
883
297
794
40
568
498
182
234
414
694
813
657
596
689
853
79
117
213
948
16
814
839
257
346
636
176
814
449
318
78
723
998
965
309
603
925
875
578
174
695
715
498
401
90
596
671
116
722
949
547
364
537
172
203
814
309
144
267
288
InsertionSort - 0 steps
460
474
352
750
268
875
372
538
822
638
543
780
517
840
656
909
868
754
594
704
696
167
186
798
12
471
304
38
100
35
437
746
505
109
166
578
128
701
752
302
862
535
450
827
2
596
458
972
684
103
969
816
926
114
223
755
731
432
85
353
259
458
811
706
367
396
326
497
994
362
870
653
215
45
77
113
277
194
650
205
426
471
807
301
715
662
454
650
918
143
20
444
497
106
939
724
574
601
142
545
ShellSort - 0 steps
733
378
556
617
56
305
317
480
243
26
520
818
513
3
63
691
715
893
778
672
987
84
536
601
893
118
744
900
953
318
232
3
208
98
845
465
960
912
67
460
923
959
661
855
383
126
766
395
530
492
812
821
612
523
780
142
409
218
274
703
150
299
283
264
438
915
413
587
900
133
245
87
386
441
310
496
581
120
668
206
856
261
742
191
948
678
45
615
491
388
814
631
407
269
422
212
378
466
61
179
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