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.

568
19
851
941
110
291
305
843
31
536
755
911
123
110
332
696
765
372
683
622
25
749
257
127
710
431
141
297
600
441
839
681
90
856
457
624
595
463
768
717
346
232
189
483
629
715
25
283
989
890
354
509
519
106
951
815
576
456
870
761
19
582
881
575
163
395
416
133
654
240
530
947
722
297
220
264
150
265
425
712
621
398
52
68
260
801
36
410
958
552
217
128
246
897
913
615
932
36
153
496
BubbleSort - 0 steps
872
235
201
693
851
544
384
143
222
258
976
838
393
362
910
40
424
535
96
643
193
878
617
152
192
687
410
781
419
827
905
340
106
409
847
936
510
90
502
509
76
888
232
750
163
307
742
806
378
340
836
240
60
699
161
81
825
56
669
974
827
328
759
561
88
796
340
17
176
9
917
212
241
134
195
121
204
524
212
118
316
623
412
906
887
75
664
336
30
882
862
729
849
668
247
986
411
374
811
872
InsertionSort - 0 steps
886
919
771
173
977
168
369
724
749
848
923
850
939
479
963
141
985
1000
791
717
26
53
846
528
126
607
346
830
263
869
933
258
898
92
355
331
198
91
442
252
868
139
783
238
815
432
180
56
962
44
598
455
693
805
772
796
509
261
491
853
687
286
381
670
755
566
89
58
147
746
768
253
116
381
986
825
406
595
61
304
975
489
762
661
693
317
2
379
286
424
433
896
109
650
144
481
465
598
74
879
ShellSort - 0 steps
113
960
827
936
111
41
413
933
940
178
511
321
523
657
833
27
371
496
535
650
734
851
5
432
277
642
626
393
148
663
713
14
549
238
78
495
21
184
271
937
140
19
74
634
928
743
463
119
26
998
841
168
555
47
338
997
748
857
390
799
556
280
576
361
220
467
884
441
470
616
394
551
394
323
731
626
852
492
130
850
699
947
242
788
183
764
750
299
530
45
567
898
100
347
415
581
425
373
531
839
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