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.

488
348
577
417
642
502
962
457
510
849
206
517
148
270
502
424
790
580
337
621
478
700
56
43
706
970
101
113
226
196
73
354
979
818
372
830
217
45
756
942
141
866
232
107
194
363
691
580
361
567
139
204
587
202
826
248
134
917
244
194
398
859
33
28
21
334
667
708
280
764
360
429
679
585
63
992
405
150
426
952
808
758
623
983
895
454
466
649
276
352
661
888
944
462
901
947
603
129
460
633
BubbleSort - 0 steps
613
447
420
441
378
764
148
766
249
718
917
667
255
297
126
166
909
783
633
496
557
178
821
91
108
474
288
820
182
161
503
22
5
514
589
461
862
594
818
359
640
146
176
970
767
596
303
82
889
915
736
667
436
5
890
765
479
815
798
836
484
523
97
214
196
665
388
39
685
146
517
806
212
299
794
918
98
776
391
484
184
173
997
518
54
25
548
811
779
183
666
610
205
210
616
843
554
813
982
49
InsertionSort - 0 steps
428
173
348
396
985
566
588
262
61
30
167
740
727
460
755
487
229
263
501
346
278
799
731
470
978
963
740
569
385
287
482
738
629
64
211
985
678
656
655
435
47
729
384
474
301
168
258
397
386
398
702
305
698
494
993
918
788
630
527
750
615
955
564
647
343
843
392
466
79
209
806
366
349
513
291
143
730
194
403
787
79
209
253
89
360
310
656
232
866
224
618
106
132
553
183
764
720
147
210
514
ShellSort - 0 steps
505
891
381
669
861
381
301
141
206
41
774
254
44
461
342
975
515
178
924
160
370
79
159
114
100
426
328
860
471
565
245
288
212
111
632
208
332
544
493
792
448
412
673
613
130
970
210
175
407
696
558
777
731
950
91
265
945
241
633
183
643
920
207
800
820
691
664
148
771
96
64
594
227
412
927
448
477
864
992
369
158
569
4
36
899
391
608
271
77
230
806
941
536
399
59
622
9
655
631
853
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