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.

740
267
901
378
802
801
315
98
118
809
117
500
155
194
864
280
177
301
403
592
477
84
910
156
649
201
23
75
772
13
874
178
709
6
780
372
900
628
228
527
775
725
810
57
793
465
97
875
364
469
370
497
179
757
496
34
515
538
266
982
684
381
128
136
569
682
893
425
37
107
602
303
282
201
969
349
256
825
25
492
345
18
499
116
660
502
257
146
39
72
563
655
984
402
279
315
863
610
380
302
BubbleSort - 0 steps
250
138
47
897
204
663
155
506
341
851
75
760
441
788
743
83
328
111
352
746
58
639
312
688
710
930
512
695
960
593
697
769
899
707
517
812
877
988
768
897
90
196
645
822
466
736
754
620
120
365
280
461
323
894
962
648
456
776
756
594
435
171
439
198
148
815
844
808
250
491
198
459
145
395
571
942
188
819
228
996
661
520
944
780
968
812
13
395
775
168
223
918
418
718
203
258
666
355
955
938
InsertionSort - 0 steps
741
625
271
910
854
411
152
326
573
193
314
732
729
165
150
477
824
205
948
938
27
499
409
564
881
7
881
317
834
35
782
897
221
536
49
598
480
509
564
385
526
54
728
125
999
188
80
984
680
425
937
557
919
451
61
152
897
844
660
880
823
364
760
954
548
70
58
474
597
269
840
109
495
688
541
578
111
779
846
686
540
854
581
904
196
994
327
554
405
360
187
115
23
250
768
563
145
428
803
431
ShellSort - 0 steps
861
137
285
371
605
780
198
738
576
856
73
863
833
970
205
306
714
72
331
336
624
625
12
118
494
640
688
743
594
165
580
663
833
821
68
694
426
419
12
298
244
177
464
899
435
445
427
667
473
497
663
709
567
904
637
671
144
165
895
978
661
562
590
800
328
65
231
46
727
318
339
808
824
56
702
484
69
764
996
556
36
385
243
491
896
623
778
36
792
968
591
294
290
673
659
830
9
216
690
131
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