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.

178
749
162
208
732
516
92
958
966
556
95
259
44
758
435
133
749
224
247
786
881
1000
278
485
213
238
805
883
740
83
685
482
103
592
952
52
806
739
163
437
27
704
920
450
892
575
158
648
935
207
574
391
874
791
541
831
653
940
410
867
260
154
684
414
352
344
274
190
294
470
702
669
408
22
786
869
614
866
222
639
623
15
989
621
787
605
231
574
953
838
546
997
736
401
698
406
636
309
70
511
BubbleSort - 0 steps
630
959
740
542
873
368
627
202
513
157
445
659
837
110
197
63
576
117
454
455
928
633
411
821
295
569
769
64
953
902
701
337
392
132
339
194
652
940
131
548
731
269
65
825
634
655
733
407
468
595
666
89
215
690
816
710
380
14
623
850
387
578
730
215
811
82
605
805
968
356
635
993
29
863
755
624
917
982
746
922
862
628
879
821
930
713
923
771
707
209
793
465
950
917
698
795
827
139
365
246
InsertionSort - 0 steps
499
897
467
191
317
254
829
952
60
852
173
890
636
796
689
707
772
549
419
501
940
363
578
143
749
389
863
929
202
990
355
730
601
417
344
757
765
261
857
957
835
140
162
814
754
828
433
583
113
351
636
829
448
313
782
654
396
66
514
139
94
747
458
798
867
267
924
936
685
191
848
472
802
58
838
924
789
752
414
516
532
395
349
468
590
190
616
292
679
814
617
718
610
806
68
762
254
12
493
633
ShellSort - 0 steps
768
907
606
338
726
78
583
590
195
463
560
177
881
348
563
805
561
6
270
933
799
858
897
306
981
464
273
758
679
57
431
88
182
639
448
182
929
242
724
756
219
931
815
78
306
311
127
354
450
699
702
640
846
144
811
701
705
757
647
824
81
428
316
949
398
724
694
244
184
868
481
473
177
646
706
646
493
934
610
593
372
755
922
539
578
547
983
159
912
376
384
902
708
688
487
14
72
303
451
631
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