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.

467
458
844
486
784
341
545
811
975
509
597
703
689
816
209
383
254
800
986
724
185
676
983
850
119
756
272
507
617
981
531
386
189
153
304
166
923
245
895
195
34
736
907
108
314
397
204
526
846
853
269
921
427
339
945
634
98
374
536
752
844
96
598
422
886
1
554
759
320
121
286
565
189
627
94
747
818
762
884
15
626
469
715
159
393
731
260
751
193
526
364
957
346
722
392
487
599
609
462
913
BubbleSort - 0 steps
573
467
485
55
793
634
6
329
705
331
381
713
5
942
668
815
256
707
233
654
623
966
172
27
170
477
47
653
678
279
465
677
797
791
38
578
53
519
681
346
32
763
412
946
552
173
531
301
759
297
668
651
427
256
62
146
589
592
615
642
44
929
814
879
715
14
329
163
831
810
777
514
811
766
221
960
383
62
738
29
264
174
637
420
394
16
912
789
284
462
806
8
753
7
41
766
951
249
464
536
InsertionSort - 0 steps
807
98
39
334
697
702
508
391
958
926
809
951
814
240
966
359
818
158
877
129
554
733
278
162
876
677
884
955
84
223
126
486
433
894
422
580
754
771
337
922
492
610
21
37
237
163
803
88
585
785
172
273
601
697
701
283
334
841
24
150
830
727
915
678
445
509
3
615
603
392
925
655
952
151
208
39
58
573
246
776
744
340
100
67
783
445
804
249
682
753
124
230
379
779
701
573
231
554
405
182
ShellSort - 0 steps
551
540
415
230
900
747
28
151
509
737
560
72
564
396
779
347
864
942
103
848
980
978
50
23
848
753
589
312
842
207
647
465
53
197
653
674
419
331
565
378
589
30
327
15
226
8
162
551
595
232
160
481
35
107
308
134
536
552
904
94
232
702
669
359
292
668
778
773
122
546
495
889
268
592
790
485
100
227
337
366
247
515
707
232
342
761
580
438
971
220
327
546
632
168
545
990
788
505
33
873
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