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.

74
950
133
246
388
766
281
810
801
785
578
853
918
542
54
369
570
468
753
582
998
44
218
679
541
806
213
208
38
435
893
702
783
300
884
549
791
562
904
906
361
735
766
73
494
460
615
893
271
542
1
875
872
953
974
417
314
748
636
731
717
511
496
482
278
575
192
268
647
142
489
477
728
147
208
568
159
501
869
734
22
762
699
817
885
722
687
460
208
698
694
429
298
100
873
511
879
338
127
199
BubbleSort - 0 steps
121
278
627
412
448
14
751
57
471
879
575
250
455
147
968
681
503
171
682
85
885
502
348
433
396
490
442
278
603
519
909
117
805
17
409
860
898
229
264
785
619
973
748
743
736
673
860
917
808
482
457
440
347
903
110
474
81
318
447
136
437
590
589
923
480
917
435
671
819
125
716
852
103
158
345
69
122
375
697
365
177
600
165
471
36
21
253
876
531
447
577
30
383
128
264
712
197
684
94
593
InsertionSort - 0 steps
398
779
637
692
915
783
519
612
401
580
539
129
297
998
833
84
991
149
369
521
135
529
24
15
553
533
152
500
71
110
573
413
890
291
529
579
98
676
801
983
64
822
902
287
206
241
90
252
118
899
779
734
137
700
379
896
681
151
657
732
574
737
90
114
819
226
578
913
17
906
806
235
103
491
595
119
296
755
281
977
829
583
277
671
643
238
895
99
553
693
798
425
347
541
956
364
36
190
362
921
ShellSort - 0 steps
164
30
220
891
669
909
588
614
467
503
556
965
100
520
784
915
325
103
879
797
589
150
902
282
133
412
965
51
941
141
249
850
449
964
190
590
463
533
735
290
956
503
650
596
126
980
719
145
896
449
575
4
484
106
456
96
660
360
93
34
527
754
442
303
513
836
312
341
333
587
833
317
573
983
190
80
457
346
662
972
263
474
923
544
385
269
843
465
955
736
449
938
581
39
140
589
771
131
317
740
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