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.

561
635
71
713
8
596
734
59
157
698
187
882
163
961
532
216
482
171
942
917
793
383
760
518
46
693
801
79
187
380
997
706
238
676
938
903
506
534
585
903
603
817
658
234
496
405
895
221
106
546
589
605
360
485
404
238
923
901
376
398
278
95
892
196
689
563
861
123
34
223
226
760
135
263
40
88
756
896
331
733
934
468
327
136
913
351
812
278
819
114
43
52
410
233
859
924
796
59
698
895
BubbleSort - 0 steps
106
921
632
668
375
408
955
147
835
103
416
161
288
809
691
3
376
75
736
475
357
439
240
584
871
840
828
907
905
481
616
719
685
389
416
205
521
181
100
326
517
380
42
185
827
460
638
297
863
245
779
473
999
719
572
774
942
200
218
551
298
421
622
340
327
598
732
817
994
333
393
833
955
72
211
555
462
695
468
117
980
565
600
379
858
776
412
197
417
311
700
770
537
60
738
96
15
418
573
790
InsertionSort - 0 steps
243
16
611
599
274
366
34
845
312
841
136
891
124
914
858
310
956
438
408
685
112
3
762
295
255
146
681
931
334
673
378
218
951
966
219
952
800
60
62
38
120
357
854
797
730
344
832
356
918
448
77
146
72
267
596
905
217
761
351
614
753
665
900
902
177
720
81
6
819
269
568
334
855
210
128
989
737
522
252
451
301
556
232
520
847
847
304
700
495
582
919
287
162
608
516
323
607
745
34
554
ShellSort - 0 steps
497
915
985
17
737
24
637
445
822
430
349
920
869
68
143
345
465
863
125
938
353
87
113
210
229
191
801
44
413
393
210
54
214
398
588
945
254
358
377
613
377
941
544
653
616
198
84
587
701
479
135
573
969
458
693
109
663
560
377
516
319
873
941
353
813
824
356
377
885
304
352
842
109
526
187
574
868
568
617
81
135
404
503
709
548
140
603
52
118
882
271
804
621
943
138
701
72
318
971
511
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