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.

766
681
288
784
912
984
656
235
254
159
548
81
384
86
139
398
83
540
833
247
508
696
740
705
898
904
624
220
377
11
496
178
532
640
739
79
355
241
223
313
248
691
356
590
885
240
506
863
875
880
499
149
106
637
648
112
807
652
552
249
612
866
23
245
968
905
785
546
697
201
119
545
377
107
460
315
626
200
177
524
950
868
844
481
824
231
396
516
596
933
957
553
237
471
41
500
811
157
116
318
BubbleSort - 0 steps
782
840
872
806
687
181
575
179
285
30
661
954
550
652
241
331
522
428
577
878
395
662
509
556
563
247
928
331
525
555
884
383
897
33
772
897
698
871
42
496
66
228
463
366
891
520
14
650
253
36
929
837
115
891
920
88
263
467
112
358
50
314
311
813
332
251
991
414
286
111
406
613
994
263
78
846
865
470
130
187
707
8
262
148
528
353
338
911
512
687
452
979
288
860
917
973
67
447
284
626
InsertionSort - 0 steps
150
520
322
678
814
825
627
130
818
937
221
918
60
681
58
143
174
150
372
902
666
891
311
275
898
670
456
59
391
465
383
702
391
452
506
100
456
524
756
693
156
352
477
456
203
358
102
524
224
374
524
676
613
567
17
596
33
547
80
23
517
787
817
603
166
673
840
794
900
382
564
870
805
162
798
200
620
628
917
9
741
908
341
687
58
940
405
21
742
619
908
935
220
614
763
453
628
655
922
521
ShellSort - 0 steps
135
172
498
226
377
546
143
256
860
355
802
537
965
99
494
363
832
574
937
918
500
84
331
798
436
590
19
166
997
672
858
429
415
957
592
270
591
899
276
611
345
574
720
560
237
85
694
828
877
180
887
193
25
322
514
253
702
84
200
517
80
654
992
842
599
106
571
3
615
643
110
509
142
70
216
351
261
769
832
340
37
446
268
748
419
578
978
613
414
541
490
689
100
897
174
722
740
815
921
889
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