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.

260
496
701
289
417
364
729
531
598
249
208
855
976
742
310
595
487
840
268
3
242
834
181
813
994
835
912
162
700
992
406
796
807
631
53
176
275
86
958
298
841
90
791
205
744
857
630
665
290
799
55
466
967
40
827
872
257
500
524
706
819
558
911
31
389
395
207
435
571
867
125
425
190
666
339
164
670
153
867
254
598
991
651
737
609
59
191
156
97
902
331
554
159
497
276
571
905
102
256
249
BubbleSort - 0 steps
415
120
188
887
522
188
865
534
158
19
714
340
372
423
529
640
88
763
418
31
75
257
659
110
948
422
817
483
723
961
883
623
4
730
339
696
414
474
496
363
119
990
714
183
906
823
575
113
613
74
960
499
994
408
152
147
954
710
692
800
893
665
716
564
427
158
469
384
874
374
300
788
688
305
529
844
571
590
363
720
196
819
270
29
125
31
865
955
315
179
151
859
872
416
863
173
832
658
854
148
InsertionSort - 0 steps
148
977
449
970
568
293
826
635
528
583
267
538
72
645
741
171
758
106
485
327
975
545
983
334
28
727
27
314
650
579
221
694
209
627
458
721
55
656
119
613
884
1000
581
276
581
548
604
562
356
514
763
920
643
633
455
70
533
426
909
690
533
528
124
580
311
317
495
699
567
650
614
281
889
339
578
590
166
796
825
478
343
18
668
562
860
770
591
988
166
468
717
713
726
358
643
68
790
452
347
385
ShellSort - 0 steps
195
331
485
192
157
931
736
39
20
811
686
603
303
49
317
26
546
533
519
649
484
559
750
715
124
691
55
815
4
461
544
31
332
321
547
904
685
818
545
144
579
562
589
453
97
807
710
279
614
99
647
350
133
934
647
98
830
875
804
111
540
187
65
236
718
608
234
151
150
380
569
943
189
179
677
576
764
84
900
385
547
591
941
454
505
561
542
571
726
349
402
891
730
34
909
636
438
991
987
977
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