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.

645
85
498
267
782
224
225
181
253
705
170
197
984
130
453
605
72
208
241
902
502
178
32
797
142
292
21
954
900
243
125
496
352
934
486
83
895
84
172
281
228
718
603
504
255
87
908
319
250
996
666
176
798
484
557
804
829
532
904
962
630
700
543
523
378
640
316
475
590
425
3
420
979
148
986
719
945
948
42
206
998
136
384
227
910
22
33
609
672
390
454
451
9
475
346
795
932
555
679
435
BubbleSort - 0 steps
100
687
622
904
720
485
696
53
400
90
623
631
587
771
588
427
235
317
641
952
597
805
769
420
786
266
807
482
843
32
897
406
290
121
514
782
275
7
159
956
495
869
847
516
11
873
68
704
86
398
804
401
962
627
815
721
585
114
889
513
853
998
382
5
622
194
81
544
187
632
529
341
297
398
72
725
47
257
484
364
768
663
60
557
34
128
986
8
336
529
960
278
520
117
918
604
182
907
183
784
InsertionSort - 0 steps
889
595
63
258
613
242
743
992
193
53
191
808
838
975
90
694
84
479
217
126
369
800
449
887
103
395
231
61
15
975
153
393
752
651
648
682
1000
345
24
45
52
867
202
431
207
564
434
242
230
199
849
877
585
356
246
98
889
746
679
460
334
306
818
888
10
288
391
111
555
271
343
940
888
865
694
983
864
600
840
17
245
491
934
914
691
650
151
629
702
497
844
30
740
41
644
771
936
283
848
905
ShellSort - 0 steps
263
451
63
597
447
835
36
344
570
837
969
568
580
267
146
180
691
451
479
342
210
150
511
123
770
464
765
920
707
698
230
192
52
247
649
529
581
272
947
516
72
631
665
254
889
193
878
670
538
867
534
52
895
693
996
440
732
819
226
296
86
591
386
699
756
697
266
718
429
524
425
657
684
697
180
572
128
453
655
623
942
590
470
487
472
637
660
136
397
919
343
571
255
546
556
17
599
202
76
797
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