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.

564
965
220
63
264
636
161
274
815
961
470
778
276
476
511
618
803
289
307
775
969
857
751
852
510
20
356
172
797
159
830
613
946
580
934
546
373
961
270
812
610
237
984
671
273
293
206
3
745
767
65
488
897
824
949
17
302
609
953
941
427
656
647
245
226
667
653
360
391
208
494
860
80
424
533
845
628
320
282
999
986
653
636
199
666
27
802
69
711
361
528
637
167
188
520
754
265
389
117
954
BubbleSort - 0 steps
392
577
190
858
485
444
911
172
292
872
958
957
662
952
210
849
783
670
700
573
230
863
14
528
500
317
724
971
841
785
367
808
396
607
673
464
344
573
332
859
982
84
740
606
504
149
955
172
88
54
190
971
146
261
23
376
861
321
700
281
659
118
370
709
964
493
864
154
300
813
660
453
156
296
309
655
369
819
806
812
185
47
402
183
975
617
204
595
724
41
40
895
772
530
487
942
886
916
358
384
InsertionSort - 0 steps
86
783
776
744
828
34
680
705
668
644
809
880
139
606
947
324
58
568
67
729
890
734
524
665
200
173
622
119
909
57
745
399
434
977
117
28
56
753
617
513
637
826
287
476
525
666
858
687
435
662
71
430
459
696
850
566
38
352
90
383
56
2
632
156
55
9
296
609
158
245
894
152
5
643
824
600
447
164
775
798
553
24
15
17
65
253
667
952
510
644
734
775
609
494
261
941
28
391
836
849
ShellSort - 0 steps
56
385
542
247
930
842
125
391
631
764
822
270
154
673
86
310
405
524
245
533
941
511
607
869
471
729
722
600
996
245
602
940
253
854
430
35
42
864
724
988
193
581
635
600
559
712
687
673
293
823
502
578
689
784
458
969
267
970
768
70
370
212
524
609
481
85
722
629
453
274
610
615
782
80
273
75
191
944
446
36
976
435
164
821
968
45
782
929
437
296
549
593
685
583
759
125
589
969
298
659
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