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.

56
208
17
183
111
837
729
439
324
530
921
424
120
592
446
516
258
807
951
156
744
612
849
776
258
271
415
101
639
200
502
153
868
211
102
852
751
96
242
406
49
935
77
713
296
825
821
922
444
856
549
651
71
637
683
539
216
692
169
815
742
374
197
534
765
824
777
563
314
116
46
214
668
577
613
102
131
61
118
71
53
616
323
370
395
545
578
622
321
440
233
652
379
165
64
129
694
439
944
244
BubbleSort - 0 steps
954
556
634
852
216
446
995
660
795
13
997
462
503
657
795
661
308
20
974
774
494
596
818
648
624
462
936
816
426
29
100
816
347
152
498
278
463
710
68
938
305
576
71
735
90
914
931
965
777
969
401
148
169
874
78
701
562
935
207
801
710
842
996
909
526
422
583
505
569
7
728
877
605
927
197
295
657
348
849
319
663
126
315
658
368
485
649
115
328
428
861
749
645
15
469
259
306
400
535
55
InsertionSort - 0 steps
835
478
752
436
63
9
145
153
534
990
125
228
674
410
49
457
668
808
789
319
779
864
654
925
480
717
245
352
796
159
367
18
569
312
257
930
173
927
672
662
325
702
589
203
59
165
103
941
560
933
911
832
951
108
935
759
938
367
541
595
523
340
133
760
317
815
535
592
212
290
867
606
512
223
372
100
325
398
521
452
735
162
223
305
905
66
287
967
654
407
737
642
507
249
69
254
969
658
68
481
ShellSort - 0 steps
402
403
160
877
955
338
809
674
632
663
96
289
396
967
594
663
689
11
791
351
104
728
56
134
918
485
995
559
351
920
401
329
323
87
435
482
130
153
112
638
954
331
146
978
13
292
862
959
720
879
837
896
676
703
971
936
106
568
864
27
500
419
289
660
474
177
534
650
423
157
473
136
946
637
509
266
406
972
600
36
866
77
852
493
524
908
656
569
281
559
937
365
125
54
296
128
517
85
646
139
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