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.

911
623
438
687
602
229
220
272
215
518
534
250
322
5
989
470
880
483
131
544
720
186
231
85
788
409
291
469
909
778
550
145
578
831
464
179
790
222
901
874
40
316
913
402
352
450
642
252
937
833
512
38
683
874
310
345
246
857
132
131
997
122
482
366
521
255
867
503
811
110
532
298
974
29
812
323
419
898
505
700
174
956
152
534
744
263
993
639
56
97
158
344
104
551
218
591
951
452
787
430
BubbleSort - 0 steps
269
801
741
694
766
748
253
250
122
423
978
885
352
616
878
972
653
330
266
477
505
226
622
446
593
125
844
737
582
715
2
974
432
686
728
149
287
944
996
219
589
887
844
220
179
322
487
55
797
301
25
463
506
635
324
344
295
413
735
560
689
258
208
878
903
457
671
36
902
988
184
61
184
797
151
936
166
59
971
555
142
447
802
975
454
662
870
220
863
551
407
997
304
100
500
237
107
710
909
321
InsertionSort - 0 steps
417
752
382
965
945
132
168
508
177
266
925
702
636
846
721
330
987
670
822
406
211
948
181
64
619
162
698
775
860
870
580
167
711
877
924
12
209
783
66
450
102
370
842
795
645
823
710
96
233
379
129
939
142
802
586
527
548
753
546
332
373
353
852
98
979
904
422
752
555
852
962
680
387
550
296
13
707
167
977
1
370
583
955
654
223
391
109
505
270
141
649
107
146
83
265
454
654
158
975
751
ShellSort - 0 steps
50
301
846
54
360
415
2
58
310
740
54
580
863
170
712
679
589
25
362
603
14
901
158
805
6
73
42
512
377
69
653
612
928
917
266
501
568
539
234
929
256
442
647
10
847
877
828
291
244
322
469
326
187
884
777
638
258
462
491
576
628
991
426
662
168
8
161
9
986
151
261
454
299
548
307
186
54
6
417
612
765
845
216
3
911
8
76
327
435
407
83
644
863
808
514
238
281
211
781
576
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