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.

668
11
276
907
624
653
957
211
400
106
327
281
155
123
627
2
563
529
837
374
436
153
504
443
79
492
832
943
936
805
62
122
988
882
312
750
587
958
825
664
738
62
677
544
372
963
120
363
553
966
52
246
624
316
152
575
444
21
782
186
70
218
84
360
775
182
375
152
726
489
367
341
4
390
631
90
840
748
880
784
43
933
733
146
917
611
246
247
840
338
952
460
697
892
913
865
499
61
247
738
BubbleSort - 0 steps
626
681
486
398
215
158
354
779
219
555
541
308
329
644
480
459
310
11
208
308
101
114
198
182
187
544
222
578
96
26
179
938
54
499
270
851
82
307
623
571
718
372
642
925
131
4
961
428
754
277
79
561
152
363
817
770
655
793
150
898
706
215
558
573
291
998
122
745
909
762
865
175
920
524
610
71
407
465
161
478
748
394
788
901
278
340
242
569
774
611
300
191
63
613
353
971
168
857
52
760
InsertionSort - 0 steps
637
989
796
304
771
67
287
78
90
453
284
407
196
134
449
436
861
198
247
765
50
596
229
221
728
120
823
851
474
293
682
752
333
411
836
403
292
703
215
147
771
348
894
762
779
383
178
730
67
147
166
598
972
715
76
685
121
267
496
338
28
964
103
670
817
158
463
588
188
281
390
996
30
690
820
271
271
875
911
499
850
630
135
930
622
130
254
873
56
191
483
856
678
305
803
512
957
818
291
991
ShellSort - 0 steps
385
111
822
463
723
662
875
368
420
927
861
16
81
452
637
247
135
40
817
694
615
809
319
680
396
640
216
252
262
218
386
51
640
54
188
500
234
956
402
744
696
12
642
912
827
751
909
937
596
137
121
913
6
765
769
854
333
994
54
456
452
276
156
804
374
811
618
639
826
216
170
147
462
990
466
396
429
178
43
658
402
883
660
681
412
841
214
669
61
865
882
487
66
205
519
675
896
111
983
592
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