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.

417
432
109
281
572
934
968
469
591
739
448
100
595
844
979
402
913
434
580
302
254
173
661
414
752
629
875
527
699
694
784
874
820
731
481
242
996
147
731
777
357
920
416
602
696
532
462
523
540
227
703
516
707
689
120
759
161
422
466
871
963
796
901
549
398
56
644
387
286
565
117
721
716
167
745
95
486
699
438
10
701
736
729
15
115
177
494
240
897
769
590
307
882
988
317
875
522
648
235
6
BubbleSort - 0 steps
662
106
136
143
627
734
26
692
131
359
220
351
902
395
682
149
338
999
57
771
420
688
506
138
451
654
862
808
974
694
496
245
692
266
640
510
14
360
50
872
631
766
728
524
302
805
233
470
340
367
505
791
671
649
492
649
670
950
372
861
644
973
935
281
806
831
883
414
168
508
237
547
730
939
774
451
689
887
439
174
678
552
838
607
790
737
395
853
714
785
507
958
530
231
924
616
745
397
365
691
InsertionSort - 0 steps
403
887
684
719
863
165
965
311
324
715
856
516
256
396
985
236
40
64
921
830
820
284
476
480
610
685
562
321
836
61
807
540
468
774
752
61
333
342
798
329
202
719
80
934
964
804
696
863
734
707
553
808
190
748
61
990
921
489
669
578
812
144
325
644
83
278
944
743
593
126
177
868
543
983
658
381
144
697
985
145
997
336
546
920
547
936
889
366
658
702
31
192
65
542
675
582
918
430
713
996
ShellSort - 0 steps
436
654
764
706
790
416
937
818
298
666
946
571
918
26
463
794
950
517
239
433
248
875
716
315
473
269
967
927
801
212
212
720
734
783
831
229
261
231
853
992
202
18
674
362
941
581
465
250
814
732
877
596
937
629
259
625
339
459
70
867
742
466
41
69
311
640
475
688
729
285
892
411
956
94
99
900
511
610
124
265
435
634
799
350
848
138
806
908
356
420
507
306
111
602
781
440
833
633
690
163
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