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.

453
863
618
879
585
538
709
210
943
847
478
532
313
45
679
74
714
910
777
294
85
978
570
230
622
796
849
588
400
825
193
226
537
344
172
808
401
603
677
472
407
83
542
786
357
994
638
456
380
899
326
972
832
517
889
286
146
724
542
351
779
868
946
352
622
243
560
34
883
53
741
926
718
156
721
538
13
428
998
538
874
409
573
832
752
979
464
180
592
610
93
196
935
30
730
469
631
918
229
964
BubbleSort - 0 steps
530
845
217
905
948
180
617
874
725
460
349
932
121
939
352
685
201
407
50
340
77
677
297
26
567
765
507
108
745
173
813
41
772
569
522
970
150
686
419
195
59
817
622
876
922
565
879
379
617
154
887
502
636
887
981
36
750
276
182
965
40
212
443
496
469
61
65
531
544
427
46
172
317
345
623
518
502
734
829
344
234
590
72
965
137
170
235
817
867
186
235
666
359
181
607
481
429
827
986
364
InsertionSort - 0 steps
582
791
689
628
43
42
809
905
52
746
233
926
356
538
193
929
828
168
462
116
218
120
73
20
244
15
96
755
915
433
96
765
632
725
625
268
173
888
842
658
215
497
490
228
628
797
200
236
646
609
530
934
476
235
751
353
579
788
849
232
876
338
979
598
207
977
735
433
696
178
330
383
833
784
892
4
276
842
535
770
309
521
766
614
686
881
717
662
700
143
346
461
50
61
452
60
88
889
929
513
ShellSort - 0 steps
497
994
862
178
829
390
386
515
414
599
353
369
172
871
961
544
958
67
390
430
116
959
62
619
715
891
431
244
90
228
102
698
703
977
494
499
970
651
495
524
901
590
319
933
368
9
568
259
731
417
1000
85
746
529
943
686
924
98
991
356
324
857
685
715
699
318
596
676
525
63
364
985
206
692
64
974
511
507
870
595
364
239
278
141
651
185
956
272
724
936
358
874
782
207
100
451
654
21
776
50
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