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.

516
60
236
613
170
414
678
999
763
454
281
193
83
117
887
508
729
467
986
469
130
29
468
790
345
31
862
864
427
528
561
476
778
444
956
558
214
333
954
581
472
773
796
655
186
854
555
112
458
668
655
641
854
235
436
348
46
162
579
461
945
948
742
79
178
811
538
690
141
939
870
931
338
717
391
1000
945
332
529
568
344
812
35
964
666
921
274
53
550
566
419
195
939
561
31
863
547
786
739
836
BubbleSort - 0 steps
88
287
699
48
195
798
853
753
685
948
116
508
240
746
134
743
419
247
939
97
850
169
282
667
710
814
977
533
170
842
102
879
121
992
597
155
24
378
12
665
847
50
561
558
595
197
807
675
591
549
772
644
915
199
541
413
22
648
691
633
400
601
974
322
562
15
161
717
616
473
869
481
952
540
804
297
937
725
772
502
182
121
143
288
234
893
984
723
736
985
369
793
630
160
737
717
600
684
372
685
InsertionSort - 0 steps
168
119
978
328
274
206
772
697
799
811
411
975
842
624
769
99
344
985
9
370
253
2
536
291
317
628
95
924
477
978
948
237
392
830
506
613
147
690
349
315
511
645
264
59
94
981
88
709
801
443
684
710
301
996
131
393
136
566
575
388
624
236
868
914
281
951
751
832
308
311
383
837
667
670
220
799
720
524
990
730
715
448
69
937
141
289
92
446
835
185
349
738
14
482
913
713
670
153
36
67
ShellSort - 0 steps
466
787
19
917
43
205
104
753
336
526
867
745
974
946
865
136
902
684
627
668
682
866
527
837
600
501
367
367
777
321
355
401
217
617
466
241
934
563
702
342
892
899
366
910
800
667
710
108
265
920
315
589
762
21
952
216
83
86
756
677
999
556
869
170
521
924
980
443
836
874
383
896
224
854
777
132
631
769
58
707
362
64
112
288
812
28
828
909
277
968
665
123
967
74
165
937
145
195
452
353
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