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.

477
743
97
79
702
472
72
799
911
466
469
120
804
122
424
954
997
129
648
253
659
745
613
113
132
563
395
216
706
675
472
449
452
738
410
502
850
968
958
969
97
39
221
56
5
894
117
536
525
916
935
517
166
245
199
751
906
621
924
644
22
20
767
358
688
834
22
428
831
21
767
614
842
32
507
787
650
796
46
328
157
118
168
233
798
760
88
621
250
235
278
324
176
737
75
700
592
228
722
535
BubbleSort - 0 steps
74
399
218
185
396
817
873
289
16
861
103
178
627
33
526
74
655
755
986
546
699
408
65
258
513
637
273
557
744
899
128
508
891
646
276
571
869
836
556
29
267
721
748
486
65
541
406
829
103
724
244
978
943
121
757
909
289
292
477
516
176
747
910
541
569
981
900
977
731
601
875
92
461
866
838
250
453
388
175
423
690
992
71
704
807
799
279
457
926
754
868
350
624
132
528
114
288
279
706
146
InsertionSort - 0 steps
140
602
730
765
953
533
208
323
369
794
262
986
276
506
591
756
969
724
692
841
838
288
750
567
47
768
678
758
409
968
795
69
242
452
234
787
463
906
882
444
996
241
661
685
639
966
462
532
713
38
202
857
401
225
278
720
52
8
952
774
99
731
415
191
361
398
179
931
933
157
95
689
848
257
146
598
41
610
411
803
723
369
641
92
140
669
211
865
29
245
653
947
625
343
61
71
290
400
715
948
ShellSort - 0 steps
324
899
574
664
431
297
42
63
487
903
333
658
747
878
675
233
862
798
259
697
345
368
19
421
955
431
356
471
38
961
810
683
541
594
937
683
601
642
971
472
230
871
801
626
849
242
894
257
47
767
609
197
221
305
383
212
774
476
337
264
358
703
291
199
846
705
413
790
403
715
894
916
514
630
678
279
769
978
776
525
47
260
990
350
534
767
634
655
704
843
419
689
770
216
989
141
301
795
24
702
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