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.

403
405
91
732
893
225
476
108
792
627
408
111
735
316
161
857
425
200
970
565
599
757
746
733
254
58
742
917
940
857
344
979
257
643
713
870
642
481
888
639
266
57
209
970
355
55
780
851
404
184
73
176
156
921
63
760
896
944
558
923
486
733
779
371
121
262
144
47
105
957
981
793
173
113
507
963
906
592
734
232
278
761
827
520
575
634
605
33
506
384
541
228
73
316
330
47
820
459
936
452
BubbleSort - 0 steps
316
440
802
351
512
995
510
127
38
588
10
482
799
883
375
47
490
970
501
842
248
294
180
508
785
610
100
353
639
186
864
681
611
140
830
669
758
830
390
641
757
16
797
542
964
727
187
920
49
978
686
648
840
710
315
450
73
326
876
294
849
612
384
960
734
31
122
832
642
96
250
33
962
190
741
490
293
904
485
633
229
208
422
45
343
152
676
635
574
537
22
133
398
870
725
185
698
809
978
868
InsertionSort - 0 steps
270
710
115
880
779
612
980
287
794
600
541
526
512
182
486
686
365
955
66
524
817
697
752
467
255
438
867
118
764
457
690
473
588
737
137
119
451
41
383
563
591
757
893
450
939
866
284
216
33
809
906
158
371
505
666
772
831
403
933
872
447
233
682
885
466
11
925
470
532
159
297
114
962
219
28
999
9
335
93
43
245
329
649
710
551
686
497
961
798
729
927
602
308
340
65
661
552
561
579
415
ShellSort - 0 steps
743
806
967
842
707
534
780
20
27
215
309
52
489
268
891
314
278
682
779
501
979
828
996
588
638
203
116
37
78
857
629
776
114
132
477
115
277
253
548
745
134
85
704
445
77
43
273
241
964
628
653
96
788
773
228
696
770
797
826
718
870
312
923
498
852
806
818
272
960
513
971
724
353
383
943
534
829
118
422
837
904
560
783
496
670
883
554
709
746
291
463
184
816
595
543
485
162
513
555
75
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