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.

957
533
373
413
463
312
349
88
527
969
59
733
708
593
90
350
68
578
861
700
974
451
955
882
818
133
151
152
909
878
202
48
323
732
865
804
913
423
902
144
90
547
384
955
622
986
121
987
848
845
92
370
147
175
409
266
66
142
676
302
749
170
934
837
405
409
708
304
92
462
587
355
38
494
676
308
945
691
582
555
806
252
468
271
569
416
704
236
820
497
678
818
874
626
651
947
975
693
832
273
BubbleSort - 0 steps
335
442
740
627
669
41
434
456
882
519
902
887
38
846
201
879
568
58
463
672
854
470
106
274
864
173
731
520
321
769
693
529
682
848
700
80
422
756
27
673
810
971
250
988
85
536
426
345
971
713
635
130
711
59
502
774
730
117
722
341
599
765
101
257
452
584
347
851
268
133
691
485
748
475
564
377
473
467
213
18
87
55
287
711
143
467
962
937
57
373
958
847
819
223
163
11
316
235
925
384
InsertionSort - 0 steps
368
584
221
938
915
415
803
217
458
165
635
652
939
698
573
972
877
397
926
219
543
262
102
92
501
266
106
282
23
482
604
104
101
93
779
712
111
381
770
346
906
133
435
517
454
320
84
952
462
28
385
768
449
686
39
639
884
417
525
471
494
580
314
908
837
366
805
450
702
24
259
479
860
576
10
674
539
697
488
351
435
455
598
305
832
367
866
223
375
666
926
48
512
423
266
15
796
421
465
782
ShellSort - 0 steps
252
758
523
882
472
295
992
876
110
18
139
817
115
665
116
806
721
342
751
687
975
851
893
54
10
184
758
80
90
232
39
776
947
766
844
257
346
444
680
318
699
182
285
527
115
976
18
132
650
209
77
593
229
911
380
417
332
643
216
871
412
550
28
116
551
64
741
320
261
394
864
71
856
469
479
398
3
94
57
403
698
615
239
16
979
423
366
906
68
857
255
156
188
687
533
624
195
797
912
959
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