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.

137
307
424
288
251
740
976
371
632
720
106
757
303
913
617
276
535
431
113
875
737
381
295
404
700
778
56
189
767
642
876
52
902
564
659
540
893
810
292
283
130
987
333
96
648
213
139
704
321
972
898
912
793
303
579
486
583
142
810
535
170
803
502
552
133
29
268
827
83
29
243
367
555
600
716
272
164
116
45
537
244
148
581
797
895
202
456
446
803
915
140
563
599
143
788
742
275
643
687
393
BubbleSort - 0 steps
116
364
89
725
392
232
609
994
559
176
662
234
875
824
621
43
938
546
390
105
460
376
450
690
131
828
276
635
858
657
803
345
130
101
705
823
654
791
221
442
219
658
99
716
176
918
163
177
553
244
465
245
89
669
857
483
312
245
445
18
635
605
964
385
409
142
7
777
682
115
993
268
592
961
559
855
105
133
195
211
872
228
14
301
783
714
926
836
756
857
907
85
984
473
264
651
753
152
492
563
InsertionSort - 0 steps
245
460
926
646
343
260
823
925
812
162
435
102
188
115
923
925
462
931
3
535
605
939
356
923
787
554
44
27
778
68
242
33
28
900
152
180
655
531
870
298
617
554
161
930
990
867
295
306
769
784
970
801
617
479
528
537
452
563
339
196
930
63
427
958
215
532
100
824
705
463
580
92
168
120
795
976
902
795
961
307
522
689
664
357
73
299
45
186
225
554
886
704
864
521
29
889
952
516
680
832
ShellSort - 0 steps
4
703
707
899
819
872
631
597
401
895
47
687
376
47
971
465
343
240
882
103
353
111
812
854
705
89
125
977
548
727
713
991
17
194
632
649
591
814
889
54
932
83
931
536
423
772
453
988
498
587
376
335
949
18
179
410
297
567
157
363
991
615
916
137
89
841
917
321
826
154
864
904
126
179
727
969
551
777
487
942
764
730
5
833
863
858
211
641
555
412
393
463
522
622
780
190
19
29
603
937
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