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.

665
747
118
37
677
999
494
834
827
146
394
100
407
125
266
27
171
607
286
845
509
852
705
987
833
805
781
908
627
394
112
971
705
497
112
406
428
80
604
759
150
211
758
693
70
700
447
104
952
537
113
434
70
558
640
469
434
538
255
980
196
662
981
509
975
496
38
761
987
97
520
63
461
887
723
309
142
586
604
529
872
534
127
171
813
117
912
13
887
624
707
927
963
953
397
422
741
381
611
32
BubbleSort - 0 steps
928
756
456
449
813
482
910
242
110
950
921
245
696
277
72
129
130
303
993
176
866
421
502
60
856
503
801
392
346
367
374
254
770
201
276
853
853
16
302
406
807
864
385
135
778
990
928
225
389
955
581
432
988
633
388
150
759
986
734
99
988
816
980
729
659
223
197
365
44
332
957
1
362
778
927
892
407
806
596
631
430
30
88
365
292
610
830
877
384
495
415
213
790
725
808
382
273
528
209
174
InsertionSort - 0 steps
782
660
39
695
206
22
662
253
59
52
480
760
772
832
179
356
771
805
334
374
836
994
299
300
597
801
763
471
527
577
730
879
118
669
210
248
836
650
701
374
351
773
960
587
351
705
259
606
867
34
417
486
133
800
725
356
740
308
493
369
45
798
962
286
776
321
598
905
437
587
552
254
974
298
471
262
689
502
681
433
787
581
816
703
343
441
945
644
792
822
119
142
514
286
337
294
209
242
618
954
ShellSort - 0 steps
409
654
427
234
482
584
22
456
38
226
477
598
862
211
553
605
22
903
944
857
363
910
562
871
852
282
866
790
430
511
100
934
855
457
925
595
241
348
717
24
847
114
238
458
226
455
359
920
117
724
156
875
992
291
53
973
472
301
656
644
287
234
78
485
117
345
363
945
233
528
558
948
833
97
870
121
996
216
63
754
790
862
879
416
156
74
245
262
536
866
537
524
371
808
148
7
794
769
23
640
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