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.

526
547
457
843
572
17
989
904
97
314
90
423
276
504
504
932
937
989
525
178
428
379
599
317
15
860
792
594
447
584
947
160
336
906
201
735
134
302
877
907
822
902
524
881
66
563
78
673
232
916
971
620
83
640
437
935
716
232
493
407
140
669
786
555
922
474
691
656
778
732
254
47
648
815
487
133
565
678
933
653
644
872
782
242
832
438
364
5
874
31
1000
409
463
247
673
114
972
899
963
885
BubbleSort - 0 steps
232
596
696
840
917
629
930
865
572
628
306
701
356
986
742
623
119
296
780
810
195
576
893
199
886
899
224
534
232
209
215
699
737
402
31
890
77
238
274
207
393
335
292
159
996
668
328
13
143
393
222
244
873
987
123
548
126
355
578
842
66
656
28
935
384
606
670
983
580
221
684
280
588
661
774
846
571
849
222
684
436
627
183
911
688
228
730
739
311
704
82
484
138
641
505
410
304
994
868
876
InsertionSort - 0 steps
832
483
746
386
891
251
594
665
301
254
810
302
102
279
372
242
233
745
948
491
192
335
354
735
652
378
975
713
864
989
671
337
10
320
590
158
163
832
695
205
299
833
276
478
435
290
296
305
269
5
787
192
589
532
886
624
368
133
748
481
711
719
697
730
128
112
825
698
163
750
410
48
529
951
144
663
420
742
998
760
524
976
848
797
675
961
22
177
38
600
392
760
425
314
914
301
421
690
104
190
ShellSort - 0 steps
107
733
861
833
381
724
305
519
154
885
801
537
805
46
280
735
944
954
801
326
68
683
303
244
806
866
365
336
144
351
463
539
463
899
523
45
196
728
334
584
235
1000
255
148
629
533
557
214
578
657
286
879
553
580
26
236
708
876
839
647
853
185
43
710
743
736
479
158
759
190
453
271
48
188
512
216
821
779
136
19
986
530
71
841
966
3
157
83
333
199
117
15
558
737
721
622
816
871
154
865
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