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.

959
596
974
999
316
541
142
487
876
247
65
672
462
964
489
882
461
11
205
438
726
83
109
511
495
490
515
7
799
130
571
418
39
741
726
802
815
935
780
280
19
453
728
275
785
235
959
229
345
965
528
836
872
481
729
813
521
875
839
27
73
516
927
420
273
70
444
807
941
114
925
918
675
941
316
52
494
941
716
256
858
882
854
45
641
320
733
532
252
255
914
41
254
239
144
246
238
815
92
770
BubbleSort - 0 steps
282
573
863
953
13
98
651
696
546
615
972
579
959
516
205
283
179
316
810
551
206
492
931
78
324
670
350
220
599
33
533
367
483
464
104
833
548
215
847
520
636
282
657
236
145
721
888
570
850
528
684
242
32
272
837
13
196
935
350
538
308
898
926
793
390
454
568
7
641
163
443
879
243
2
576
822
892
447
833
3
492
936
820
24
982
227
707
100
449
730
944
828
516
851
806
950
194
21
720
360
InsertionSort - 0 steps
831
456
318
620
443
119
57
335
400
630
928
635
530
482
743
4
936
217
616
833
369
895
692
648
192
250
892
943
524
832
849
875
674
188
797
196
449
852
722
668
193
435
167
688
273
55
161
718
932
841
727
661
903
263
866
427
584
839
423
26
222
191
303
538
198
87
768
153
56
912
335
271
824
159
992
92
165
614
243
269
500
415
307
660
908
593
331
914
255
881
267
736
516
285
574
281
895
68
723
532
ShellSort - 0 steps
939
89
672
305
922
788
319
546
384
559
76
362
627
1
218
1000
781
692
701
258
505
473
323
810
158
243
391
335
236
498
527
875
688
724
427
11
945
123
915
85
399
183
906
900
233
611
555
919
843
162
389
971
269
469
796
350
564
423
521
443
908
548
189
676
471
175
622
184
962
325
842
826
656
457
30
18
725
518
143
818
407
140
63
516
576
652
599
126
208
549
674
476
347
140
859
223
326
313
599
102
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