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.

575
41
681
62
196
550
740
571
266
973
521
447
694
261
30
830
610
704
188
247
179
436
675
957
615
927
32
144
88
788
604
742
663
737
27
280
25
558
784
80
850
20
472
710
643
488
774
175
831
2
848
621
894
675
347
405
960
651
982
852
25
384
984
238
977
948
315
887
750
275
851
218
13
748
904
804
207
341
901
305
340
307
230
75
699
921
663
436
27
580
861
615
5
586
797
750
719
430
139
451
BubbleSort - 0 steps
421
206
670
824
162
121
981
781
565
936
453
993
929
449
113
854
955
496
439
728
807
767
8
959
395
833
216
989
719
95
731
42
61
830
160
303
725
721
767
752
350
707
476
286
561
89
844
325
904
5
188
397
778
481
986
235
82
740
348
780
45
833
807
282
125
608
865
805
461
819
539
209
835
810
634
526
240
165
380
541
7
479
50
538
895
670
707
276
86
230
716
362
208
779
442
236
93
121
336
359
InsertionSort - 0 steps
373
52
297
698
219
697
430
745
816
805
470
967
953
797
545
995
15
803
398
936
121
272
510
803
739
90
177
805
248
962
837
23
284
177
933
696
97
243
124
358
495
534
994
852
804
365
630
318
640
974
589
779
752
644
547
542
446
355
961
648
407
324
675
460
749
2
961
6
114
942
594
812
622
300
637
633
556
732
361
247
760
240
286
902
65
1
96
501
672
363
592
280
532
284
480
141
367
767
107
761
ShellSort - 0 steps
748
656
691
798
843
302
877
719
619
353
208
636
358
306
599
131
717
709
811
912
555
583
844
722
498
888
941
976
873
574
328
795
494
15
26
582
216
600
985
440
586
896
680
757
755
966
876
603
89
58
760
79
534
71
118
206
938
230
717
354
169
355
740
871
803
923
467
11
777
958
70
476
618
542
19
778
909
193
985
572
344
292
82
491
687
573
442
709
47
760
977
386
638
417
87
639
837
873
739
521
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