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.

313
573
995
290
987
92
815
493
276
851
658
162
395
59
354
221
45
569
570
464
607
264
670
241
300
319
765
939
936
104
928
975
127
434
341
803
311
955
334
761
541
75
11
252
833
130
119
532
936
899
617
766
144
316
26
542
951
244
627
775
777
939
793
455
951
805
401
9
92
641
430
252
717
423
287
713
76
718
809
188
294
619
597
932
587
964
755
863
292
962
778
525
154
485
913
564
510
113
642
614
BubbleSort - 0 steps
668
379
318
555
35
982
490
534
616
588
142
994
800
313
188
442
123
384
976
725
315
689
506
155
552
912
131
884
562
259
704
421
558
905
172
626
302
293
350
579
592
636
82
125
168
430
510
2
914
698
434
224
242
94
704
723
504
632
485
495
671
694
419
662
441
486
350
183
609
436
695
399
557
236
749
745
159
686
501
152
935
353
605
50
918
992
542
178
467
605
902
576
633
729
354
709
890
571
250
389
InsertionSort - 0 steps
101
500
73
951
10
720
156
897
391
457
431
274
28
294
720
639
39
222
427
2
134
677
437
61
319
414
453
553
816
436
310
977
10
896
699
951
311
562
124
166
570
9
987
679
683
789
194
193
855
368
557
544
772
722
664
249
924
959
395
789
229
343
75
855
161
497
211
316
696
78
557
19
172
232
959
212
305
933
167
745
126
172
442
860
639
463
597
368
730
247
926
933
189
727
722
307
187
857
898
197
ShellSort - 0 steps
567
187
624
958
956
493
722
407
621
518
966
537
1
856
634
886
344
11
59
49
50
802
897
697
501
30
591
362
235
700
838
940
114
762
534
467
534
924
37
984
319
586
641
469
575
990
490
822
65
278
895
64
823
11
826
921
562
984
219
319
420
398
908
390
832
463
977
814
648
933
172
569
408
685
527
851
676
43
170
893
500
686
840
196
172
817
180
896
165
662
167
313
681
92
11
878
849
217
938
28
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