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.

893
677
607
601
79
140
748
652
184
14
865
67
346
41
384
81
806
924
961
326
212
204
533
479
799
626
270
914
975
749
594
775
842
227
514
478
386
1
416
480
878
877
998
508
425
200
580
7
792
706
61
212
372
593
273
951
490
182
107
256
695
190
451
946
752
474
989
333
421
781
952
507
376
890
767
120
115
921
139
997
286
126
587
689
84
858
54
593
856
54
766
149
683
170
33
729
307
683
595
726
BubbleSort - 0 steps
53
790
475
888
773
413
993
286
845
486
136
751
178
907
589
271
768
613
284
116
932
699
699
7
589
612
899
982
409
742
70
292
425
418
328
117
789
696
699
559
666
145
237
595
112
294
355
484
736
697
640
59
48
908
411
716
759
428
826
817
556
382
625
556
477
772
316
171
321
55
759
380
672
668
981
972
260
235
392
843
233
959
399
518
635
466
701
969
959
771
130
593
847
946
922
611
434
76
768
267
InsertionSort - 0 steps
719
58
617
305
583
496
510
496
81
574
292
816
595
566
29
804
526
955
506
706
32
927
970
332
153
482
913
748
87
938
409
578
538
676
595
826
350
783
991
139
196
72
294
166
808
30
409
435
831
241
92
507
676
362
187
246
585
41
148
759
337
129
842
550
966
250
27
850
45
188
213
581
390
193
359
567
556
538
432
598
211
893
798
538
358
823
991
542
16
386
146
138
174
610
350
305
145
590
911
846
ShellSort - 0 steps
54
177
706
916
88
26
456
561
555
960
135
857
371
232
170
627
717
139
36
135
808
500
425
477
804
597
383
679
421
497
159
394
792
870
413
333
151
324
838
840
803
91
850
885
949
910
137
279
427
404
454
596
214
455
533
204
972
512
126
859
174
325
31
503
413
186
592
699
225
855
105
159
971
609
218
612
508
584
324
244
879
611
524
763
515
943
531
453
88
809
118
954
421
442
13
369
203
310
890
901
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