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.

29
341
89
514
267
533
955
328
577
192
578
807
757
441
86
309
922
529
51
137
351
604
215
483
503
515
423
26
587
311
744
882
895
529
204
827
778
685
908
843
352
259
168
521
988
208
782
342
948
425
862
924
800
701
644
550
141
805
150
565
74
769
496
281
89
910
299
627
877
482
333
341
667
653
797
64
907
756
918
805
979
323
283
256
755
974
31
401
952
240
299
316
15
962
737
960
930
476
479
592
BubbleSort - 0 steps
647
915
991
128
592
358
350
792
24
916
698
468
615
460
621
939
170
445
179
736
990
693
666
408
717
984
447
430
129
616
697
997
865
194
605
530
360
587
997
945
913
142
425
667
419
362
204
310
86
650
896
63
18
569
929
243
848
789
94
54
622
433
582
109
837
118
473
234
635
825
690
31
763
685
462
351
202
752
646
879
356
881
435
888
194
764
367
375
270
801
436
238
981
344
126
534
953
686
550
955
InsertionSort - 0 steps
791
724
112
161
937
587
506
799
393
935
460
84
114
536
970
526
849
714
724
702
413
439
518
6
792
853
939
746
340
996
266
275
310
436
859
31
646
824
199
639
525
19
781
284
823
958
898
261
5
231
191
936
448
368
417
759
850
808
46
385
871
441
957
121
398
877
988
602
894
869
900
395
127
554
529
179
510
660
748
761
699
28
955
416
43
775
906
50
43
64
187
351
795
100
959
187
237
969
897
406
ShellSort - 0 steps
442
849
873
588
484
688
245
777
712
58
601
587
84
516
324
3
621
343
494
528
895
432
353
432
241
368
314
933
465
268
828
519
106
787
977
657
877
924
820
353
13
983
545
519
149
91
562
861
461
298
196
159
77
830
754
387
271
238
180
869
6
971
555
757
159
755
304
294
933
948
358
113
563
265
362
404
51
310
798
910
402
524
975
435
510
517
605
441
925
474
188
280
146
671
886
136
610
262
668
995
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