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.

420
888
849
95
488
538
274
759
181
762
158
739
433
108
224
410
972
119
739
214
86
470
878
757
694
643
204
679
221
309
801
342
805
424
801
371
391
790
632
93
122
349
716
522
528
849
764
570
600
392
970
266
500
203
760
441
985
522
82
94
68
810
571
623
883
26
417
390
666
722
769
837
649
91
244
390
59
568
925
947
714
288
669
786
860
951
188
795
452
755
795
550
324
905
354
893
735
741
319
558
BubbleSort - 0 steps
492
834
761
814
677
283
207
711
401
985
612
298
19
617
770
184
89
996
70
497
784
735
415
696
660
37
333
245
102
192
419
454
696
4
792
977
922
332
360
114
715
991
549
764
749
207
791
482
511
914
575
261
195
830
950
57
781
343
999
130
537
21
267
767
614
101
742
855
952
852
169
746
804
353
711
718
876
547
803
107
179
274
66
440
212
521
447
238
538
650
273
969
213
496
797
859
958
113
387
166
InsertionSort - 0 steps
442
375
753
696
432
472
25
135
288
346
170
259
127
277
939
124
910
766
170
381
201
800
314
543
193
662
133
34
308
545
811
729
875
491
800
319
105
396
41
93
711
819
284
451
638
536
998
992
530
505
325
970
942
415
454
660
223
57
248
580
825
747
884
928
669
61
924
769
160
764
613
324
135
60
203
804
946
33
211
652
412
637
366
690
109
450
60
164
544
800
876
634
927
323
206
742
829
641
516
85
ShellSort - 0 steps
499
833
472
684
473
185
845
477
832
463
840
767
839
460
287
410
798
35
825
241
794
17
7
370
255
237
865
580
155
20
274
406
910
354
392
308
719
751
499
421
350
412
632
725
691
108
335
977
368
839
824
481
597
515
287
737
231
848
406
244
4
699
418
435
291
662
85
39
813
36
994
393
195
685
882
124
813
734
831
533
120
82
503
615
901
752
924
98
70
107
544
719
395
441
675
715
944
695
186
403
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