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.

220
626
315
617
891
621
578
390
580
787
682
25
460
840
804
428
6
159
480
846
470
728
288
962
463
934
439
762
90
628
996
650
177
437
863
770
465
536
286
320
665
999
222
798
530
57
143
233
223
705
674
333
573
802
427
207
904
764
888
815
756
891
295
176
666
728
810
695
719
624
288
153
463
499
106
58
43
620
796
259
253
603
299
460
165
912
661
613
354
746
786
856
796
819
475
281
469
942
775
606
BubbleSort - 0 steps
44
836
925
751
298
581
718
270
196
950
773
179
140
230
435
2
449
456
913
715
511
734
968
339
512
15
158
848
138
175
85
12
498
694
498
241
652
553
741
235
875
988
967
645
226
752
256
124
780
42
684
510
705
307
45
116
492
742
282
125
273
967
739
237
21
410
760
771
79
318
41
59
182
759
359
218
831
451
954
324
646
368
483
605
114
393
28
714
626
619
598
342
36
832
181
85
962
69
709
290
InsertionSort - 0 steps
474
263
807
650
505
608
43
918
396
995
139
504
594
324
330
605
437
605
705
500
484
29
84
127
522
98
93
586
902
187
876
700
510
859
602
484
42
952
644
121
892
504
98
821
582
87
638
610
910
844
793
155
72
753
466
708
283
629
435
514
768
224
474
932
686
271
455
579
684
767
841
227
400
511
713
653
991
529
42
463
983
336
933
930
235
191
300
638
668
667
413
88
537
277
896
576
816
491
606
777
ShellSort - 0 steps
277
730
556
842
825
866
570
274
970
393
196
966
681
359
558
582
880
253
208
854
750
988
827
429
373
829
14
607
920
567
554
619
920
421
231
20
489
72
163
287
451
98
355
912
372
277
299
729
871
115
844
939
358
154
987
75
540
501
153
532
212
349
788
745
422
732
196
55
752
977
353
972
510
796
465
950
55
661
776
833
244
769
169
845
125
756
762
707
876
381
884
524
70
2
590
831
402
151
942
798
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