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.

600
419
606
620
556
636
363
242
571
649
676
561
678
565
488
448
844
497
651
101
543
868
772
36
132
171
792
503
546
91
806
29
665
320
423
5
925
168
807
434
7
56
970
74
242
555
476
35
673
149
961
505
454
964
237
477
576
700
713
529
607
431
884
62
776
688
514
661
556
416
878
475
564
883
75
669
521
892
228
310
985
312
791
992
377
579
892
58
317
793
806
58
559
23
507
282
975
287
999
46
BubbleSort - 0 steps
437
992
964
36
475
699
180
935
134
195
226
271
388
382
14
445
922
873
119
528
153
879
123
865
304
648
500
747
244
828
99
380
690
806
444
179
654
428
973
469
173
999
350
294
433
548
994
597
76
329
716
867
378
314
45
243
872
997
439
219
616
251
1000
236
24
563
156
903
412
628
558
200
615
298
799
540
412
763
475
132
190
959
171
944
476
744
646
333
295
362
67
706
926
455
520
950
738
562
416
166
InsertionSort - 0 steps
400
796
509
804
283
65
458
835
572
258
449
226
857
283
382
444
886
516
798
617
18
235
436
778
470
519
122
304
87
170
24
507
600
455
735
193
125
969
234
348
672
374
448
807
491
953
194
171
814
501
829
456
432
562
688
538
635
207
891
56
218
325
508
843
577
816
254
320
962
702
224
653
147
441
5
971
31
996
515
894
789
840
345
503
913
874
502
883
104
625
637
808
444
523
804
84
317
363
535
72
ShellSort - 0 steps
37
86
593
856
777
107
713
637
564
587
3
686
892
45
751
797
861
690
508
59
403
527
146
840
584
391
847
410
171
293
306
221
657
174
269
81
108
574
510
984
118
248
838
937
903
534
894
727
270
956
553
217
971
588
481
553
890
463
414
288
228
960
165
326
712
307
538
824
154
992
99
449
530
15
18
476
217
368
270
366
270
779
385
310
574
835
489
466
609
378
103
688
245
307
679
554
188
206
388
509
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