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.

849
171
769
79
195
470
761
50
627
219
487
537
35
157
313
73
751
621
792
15
927
166
871
3
534
802
340
357
547
86
280
172
490
762
683
708
222
858
516
965
66
301
619
770
428
483
811
666
197
994
393
998
623
560
544
6
983
452
666
417
836
56
740
652
167
661
356
80
394
788
853
348
998
765
693
259
584
628
870
156
447
526
515
645
826
884
840
59
980
838
985
99
109
926
803
41
933
928
841
50
BubbleSort - 0 steps
402
771
45
710
739
419
79
641
999
229
409
347
498
97
73
42
144
237
210
549
842
253
924
578
419
451
659
883
96
957
576
589
63
939
58
394
309
175
692
438
574
615
960
605
255
928
147
967
134
781
761
944
519
277
154
685
895
147
998
696
748
476
418
173
39
298
177
488
992
881
264
159
893
513
385
966
662
923
354
471
654
247
552
466
114
939
569
223
69
766
596
901
498
951
809
694
337
379
791
824
InsertionSort - 0 steps
300
314
885
394
511
987
763
988
11
802
160
832
164
628
497
559
720
48
900
82
575
470
892
477
535
838
796
90
460
446
742
594
873
18
231
720
399
151
701
924
712
582
790
773
39
786
820
827
515
36
305
663
931
278
542
476
303
165
19
450
569
638
194
856
463
63
945
735
814
141
599
535
856
427
185
578
658
730
326
362
878
449
997
551
35
311
626
132
606
777
692
892
970
1
932
758
878
88
117
546
ShellSort - 0 steps
584
632
263
661
846
538
316
707
895
904
248
851
846
401
938
502
667
390
448
939
665
419
412
303
195
116
162
813
116
631
171
643
639
628
954
707
592
964
531
90
240
578
597
348
295
338
685
982
123
355
963
658
227
328
80
777
786
347
665
196
314
147
974
486
477
962
945
741
237
490
736
175
409
299
852
16
805
108
827
80
998
561
855
968
295
264
862
982
295
677
689
501
840
765
870
586
941
193
335
475
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