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.

844
571
65
684
224
927
595
5
831
746
9
447
427
46
157
330
937
319
918
965
579
21
131
266
618
554
209
215
620
8
751
861
309
847
952
67
882
812
837
469
788
912
353
382
365
242
578
933
188
802
550
290
532
720
393
273
424
134
565
894
987
726
30
818
710
415
432
744
795
600
63
699
838
444
451
320
650
503
246
256
532
119
271
603
157
998
228
106
484
157
761
765
321
590
489
281
585
734
678
36
BubbleSort - 0 steps
351
417
573
680
555
340
256
153
440
169
312
69
439
777
851
462
736
59
170
851
887
361
770
968
791
147
927
537
682
27
357
799
247
55
795
8
595
980
501
291
278
270
985
834
955
731
713
318
302
932
671
188
652
213
176
11
398
796
705
279
67
742
674
702
659
67
143
29
316
709
800
435
472
938
668
128
312
395
258
316
37
546
550
696
817
396
824
423
749
15
659
796
467
532
550
990
600
151
454
41
InsertionSort - 0 steps
903
953
257
535
181
370
865
462
835
372
521
593
540
204
91
461
271
723
109
415
201
263
843
932
802
129
211
234
758
470
201
757
722
783
653
957
879
181
419
620
890
456
427
161
937
391
845
828
523
783
547
383
206
175
689
934
779
216
203
13
45
674
835
264
239
430
401
112
805
163
352
631
858
428
508
9
530
197
675
738
409
575
613
458
414
813
743
840
404
117
217
371
96
262
857
715
346
681
797
487
ShellSort - 0 steps
231
954
725
838
101
317
448
669
819
534
514
238
232
114
219
332
310
605
171
49
68
581
948
286
121
628
858
959
388
573
477
671
34
856
161
821
947
171
691
229
464
983
175
554
80
735
941
667
681
518
791
84
905
747
627
145
468
665
833
27
156
582
508
255
664
839
915
710
334
951
749
695
993
525
105
568
889
969
636
556
533
40
392
939
747
236
852
81
725
774
835
402
232
180
5
727
100
770
661
761
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