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.

745
301
865
219
373
779
414
799
408
434
63
87
370
313
158
646
309
38
108
457
36
914
901
227
461
956
391
550
589
80
571
208
660
6
995
306
585
959
540
359
37
141
307
40
653
632
472
760
765
517
809
458
835
852
808
876
301
481
398
887
460
346
295
443
825
384
597
70
51
51
533
820
957
979
445
10
786
663
870
276
622
12
938
854
355
769
762
25
698
312
652
655
611
659
954
395
439
14
810
753
BubbleSort - 0 steps
830
220
740
141
754
676
940
249
748
859
441
835
160
318
800
143
311
88
579
913
311
788
573
400
530
856
142
375
371
96
650
324
292
215
117
749
986
771
173
307
757
110
566
542
1
448
820
930
855
336
610
425
220
80
539
268
543
545
716
744
298
483
532
80
662
881
601
135
73
925
904
980
69
746
896
254
736
881
973
407
983
756
753
843
425
542
671
831
543
888
790
757
682
211
564
426
988
757
850
909
InsertionSort - 0 steps
673
828
673
876
316
273
57
611
869
28
583
65
565
616
801
346
459
186
396
963
837
681
266
878
859
579
328
565
153
339
788
23
536
629
249
659
501
488
21
724
963
294
3
198
591
222
548
670
521
726
454
293
622
165
481
930
854
85
471
805
998
831
100
161
40
949
856
925
272
212
998
289
770
249
909
340
18
47
185
559
734
207
137
782
115
248
807
960
267
639
575
226
213
56
842
776
202
243
680
54
ShellSort - 0 steps
429
123
859
601
489
764
400
7
913
988
743
857
146
301
862
98
565
360
939
606
259
575
350
227
343
286
114
288
824
861
499
140
61
19
769
281
843
889
721
551
489
291
926
497
464
176
471
838
110
814
761
19
94
89
518
379
187
849
74
60
777
541
284
910
284
885
237
498
281
116
779
527
641
591
473
479
879
201
62
679
747
657
156
653
135
714
183
690
190
511
592
259
493
319
389
611
498
463
53
628
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