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.

712
234
804
999
694
763
1000
713
771
557
36
401
923
125
609
678
948
711
265
381
691
98
540
774
340
642
451
877
471
774
127
759
492
151
333
353
144
764
924
156
646
498
808
792
580
950
631
954
351
43
580
279
782
175
207
223
376
767
690
428
94
221
309
303
758
177
64
361
768
470
929
573
748
543
70
446
948
110
917
561
275
255
790
592
964
247
917
355
973
271
21
806
570
126
153
125
232
479
135
580
BubbleSort - 0 steps
245
405
940
124
216
983
239
476
410
699
469
70
932
420
647
957
6
857
202
731
608
993
822
787
82
933
302
221
570
799
477
611
601
980
760
18
186
640
697
566
671
517
476
861
532
643
609
668
548
273
775
142
381
919
279
139
146
812
782
13
77
156
598
140
985
61
118
467
566
519
969
794
765
981
998
887
606
633
734
923
89
26
135
660
849
734
219
220
775
851
649
76
387
249
465
105
965
669
687
282
InsertionSort - 0 steps
897
819
215
292
339
39
173
143
400
638
213
319
15
959
223
585
378
612
4
750
551
190
68
962
280
620
24
454
489
213
527
801
601
376
296
208
473
427
615
959
305
227
122
371
166
336
461
971
238
257
452
391
593
194
165
852
395
488
225
255
322
949
427
637
212
884
759
900
598
207
337
501
520
720
3
662
558
884
844
823
828
331
69
814
974
506
666
52
766
153
228
478
47
637
855
135
235
654
34
198
ShellSort - 0 steps
998
527
787
81
127
824
668
45
61
81
438
694
11
962
994
900
404
251
357
263
168
784
304
179
316
637
758
363
277
617
830
178
380
757
142
697
300
952
482
527
41
723
694
748
998
741
110
417
114
924
507
230
345
299
60
134
458
878
488
908
454
883
109
478
363
622
262
621
959
720
751
457
643
708
437
340
234
735
807
392
769
450
34
273
218
185
449
605
17
189
221
863
864
471
273
482
541
636
257
250
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