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.

236
156
698
843
519
94
725
686
686
446
117
604
13
593
312
179
494
246
951
770
231
669
287
251
943
828
689
978
82
593
68
720
701
699
412
685
104
523
27
893
331
905
793
956
55
548
154
832
284
788
961
395
514
636
728
302
994
528
334
248
1000
767
917
314
918
212
791
813
813
905
278
868
297
237
742
351
189
330
517
785
419
914
759
943
847
613
917
909
938
983
908
542
308
430
990
112
437
848
553
668
BubbleSort - 0 steps
997
106
564
307
789
294
94
851
591
67
86
811
382
35
928
462
282
989
368
144
162
751
986
600
734
65
191
877
184
801
259
807
275
454
160
129
553
334
529
112
809
963
811
935
162
408
102
313
415
763
93
651
523
439
992
515
498
943
958
708
773
528
725
9
246
548
853
179
27
653
602
522
12
14
844
20
472
827
319
783
307
423
883
614
599
466
518
178
687
224
671
734
567
1000
788
810
194
844
480
84
InsertionSort - 0 steps
27
301
397
949
420
549
399
829
908
385
723
718
565
957
854
149
516
672
668
654
16
15
144
425
661
927
913
508
186
243
412
734
548
978
430
976
396
249
517
581
159
103
37
560
923
336
7
921
891
533
845
2
404
69
791
925
45
251
89
298
709
974
435
583
634
886
613
44
366
726
849
694
807
203
852
39
987
810
88
355
532
230
239
273
572
728
990
632
493
710
527
240
65
761
209
922
221
833
963
973
ShellSort - 0 steps
224
517
792
723
795
101
748
586
528
822
694
112
918
377
746
250
148
235
982
33
819
411
139
402
651
791
191
472
500
85
406
347
148
503
178
863
7
357
982
709
815
560
32
612
137
483
356
466
417
167
930
368
864
123
533
214
521
610
405
202
723
230
925
656
727
799
757
659
227
484
613
714
285
408
515
777
576
601
493
987
124
723
575
103
59
428
535
31
980
97
880
166
859
884
182
552
261
850
674
779
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