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.

506
742
95
563
290
429
490
662
139
311
211
304
709
540
86
859
374
807
534
666
981
874
175
305
304
435
868
344
456
430
117
708
285
61
738
743
254
318
345
257
485
606
995
631
989
923
88
564
449
103
426
309
678
518
249
622
355
786
269
529
616
253
474
995
150
75
20
97
466
363
653
3
971
56
760
116
65
259
319
343
50
716
126
9
260
737
69
423
598
782
167
919
15
607
565
436
777
294
108
142
BubbleSort - 0 steps
589
877
817
440
304
391
582
757
999
3
981
969
911
159
444
230
410
691
832
196
814
204
815
239
131
479
863
219
919
143
998
407
63
11
695
644
905
498
188
789
22
70
159
77
596
739
14
496
885
209
224
60
392
731
515
268
18
547
72
222
700
874
659
298
34
889
801
342
495
940
708
8
861
936
343
394
841
930
657
14
928
724
311
228
845
533
110
833
812
98
574
748
640
821
601
954
775
681
722
339
InsertionSort - 0 steps
231
58
551
612
661
201
886
220
441
591
973
567
479
670
72
186
835
531
220
406
659
696
2
441
424
55
861
554
912
715
796
873
652
979
991
440
604
600
561
39
628
331
888
634
223
773
996
724
727
230
446
611
763
774
481
200
875
525
98
69
261
516
876
427
495
728
771
131
984
875
938
876
903
616
154
935
864
997
961
104
999
385
183
310
83
894
6
198
411
402
414
154
279
492
316
978
265
747
855
706
ShellSort - 0 steps
802
652
982
132
73
731
864
749
574
29
495
484
347
337
629
561
584
911
707
844
832
288
945
454
479
896
732
997
459
639
844
602
563
717
874
72
765
529
709
556
573
181
546
196
709
404
323
611
684
235
838
489
226
465
861
357
106
984
198
704
564
282
835
398
383
798
121
244
415
361
411
250
615
199
393
276
728
673
336
587
783
263
399
442
672
801
211
785
793
355
321
707
15
745
556
307
93
771
3
986
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