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.

214
736
916
514
12
496
802
654
680
187
12
8
852
527
350
374
551
851
310
237
171
560
799
691
845
787
140
40
990
714
498
64
184
682
234
156
195
799
950
409
405
936
247
429
562
355
979
908
695
882
212
299
581
211
210
218
700
347
145
587
931
445
887
600
377
505
485
675
90
250
12
826
287
696
190
854
370
443
185
624
902
404
728
794
857
237
940
532
499
838
303
781
453
295
546
307
182
251
216
72
BubbleSort - 0 steps
505
826
788
400
386
108
406
919
428
281
872
605
742
915
723
722
614
726
560
301
756
118
841
111
875
883
963
358
247
960
682
800
580
694
981
258
914
80
991
632
612
794
680
717
496
33
752
679
894
544
613
541
937
78
996
914
847
10
904
50
526
747
260
298
210
887
410
68
720
993
528
735
472
328
419
944
436
69
89
279
364
517
509
493
63
787
839
784
571
540
999
808
819
188
224
125
577
307
743
39
InsertionSort - 0 steps
95
578
679
408
788
86
663
303
22
976
474
43
326
187
704
493
118
885
224
329
233
451
323
777
499
92
141
926
91
79
451
678
128
866
166
979
311
967
708
902
798
167
317
146
403
283
30
300
65
560
752
371
725
543
209
446
193
776
710
964
401
34
646
905
471
472
488
502
521
89
524
576
201
994
758
829
345
646
624
770
434
277
123
166
25
772
671
843
723
344
924
887
977
408
859
607
835
763
532
536
ShellSort - 0 steps
785
589
891
425
369
414
614
399
29
997
854
768
276
937
217
938
887
293
144
637
107
445
287
326
151
11
393
54
92
751
273
445
582
43
142
597
634
992
513
963
65
363
395
273
310
478
199
368
666
265
990
306
834
584
349
954
689
863
868
73
897
307
644
31
55
81
583
683
595
888
728
920
172
228
655
90
26
277
221
30
398
800
850
650
890
255
955
610
395
514
62
848
33
792
956
774
121
722
887
566
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