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.

675
631
569
986
184
952
93
367
492
66
214
103
810
106
733
253
697
275
960
423
905
716
839
339
571
640
521
270
449
163
236
388
151
531
863
226
48
865
751
280
201
983
469
89
132
645
55
256
352
802
839
939
785
151
167
445
942
243
938
821
66
623
607
475
487
497
447
830
882
798
263
661
894
907
828
672
122
720
476
21
931
24
208
500
539
572
197
186
698
689
406
622
586
957
87
565
280
718
1000
940
BubbleSort - 0 steps
771
636
28
908
151
344
726
364
959
260
513
331
29
581
293
379
483
153
384
500
162
368
912
641
49
873
840
446
302
208
301
422
82
564
109
592
844
806
18
556
904
132
631
962
927
502
782
935
320
984
204
396
704
28
628
790
436
532
741
327
748
800
128
492
17
740
84
151
864
395
793
799
85
976
605
443
854
323
923
561
270
896
175
519
194
711
387
997
520
905
378
595
153
373
261
153
293
97
963
597
InsertionSort - 0 steps
899
340
163
827
486
727
345
316
95
754
756
786
553
953
699
452
784
882
742
432
642
367
647
607
810
214
518
462
394
113
906
737
810
319
275
724
197
200
22
691
560
627
836
45
373
258
209
901
171
600
744
176
101
985
635
802
963
428
977
267
977
27
251
490
467
694
5
561
645
708
574
270
825
214
787
306
161
220
883
507
782
670
578
942
96
704
189
698
563
819
859
965
11
995
201
783
99
409
912
779
ShellSort - 0 steps
3
728
796
410
830
225
38
812
448
561
687
983
767
864
104
623
32
280
654
816
944
176
320
616
26
351
173
893
19
146
440
651
417
41
767
864
345
127
228
75
366
196
802
198
195
103
826
843
736
874
89
778
252
521
486
849
441
941
49
569
622
822
823
699
618
284
905
316
694
394
124
487
155
631
643
105
9
270
141
766
110
510
878
644
961
946
577
954
268
799
497
843
33
591
890
46
222
234
604
178
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