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.

336
431
393
669
224
790
512
29
399
938
647
13
306
674
193
401
114
524
964
22
662
55
86
49
11
583
998
462
456
378
701
189
468
224
342
633
748
533
469
313
477
947
328
572
55
491
892
287
404
964
553
421
361
359
147
201
744
677
118
994
447
923
695
134
982
460
638
445
45
556
754
54
457
449
167
862
659
85
757
817
20
53
6
352
204
564
895
498
787
670
699
297
878
904
685
286
95
943
493
109
BubbleSort - 0 steps
677
54
619
770
640
592
424
247
594
720
466
407
953
870
468
726
716
954
168
5
78
68
247
291
141
629
512
346
513
477
490
609
414
980
158
984
328
149
557
696
821
146
816
365
440
813
450
470
719
6
562
294
262
877
325
517
122
119
449
829
181
534
181
148
594
834
212
797
955
103
176
688
789
720
675
502
73
486
342
539
766
81
391
146
450
999
971
37
106
165
604
673
333
36
267
434
245
127
11
388
InsertionSort - 0 steps
356
477
953
948
491
850
944
546
118
374
13
465
278
316
231
387
612
985
906
925
85
889
876
459
923
704
49
282
868
351
943
84
582
474
697
100
676
77
677
927
510
754
159
492
104
205
977
884
853
893
105
982
263
299
956
730
606
762
565
673
66
79
599
830
83
289
639
210
1000
474
327
24
16
400
327
127
548
815
713
981
920
203
909
83
135
242
53
829
947
479
217
961
21
721
608
104
440
779
431
540
ShellSort - 0 steps
327
468
803
861
433
682
598
788
731
357
220
460
156
968
189
765
515
538
667
155
487
204
920
666
549
651
887
494
768
680
73
673
878
771
222
265
876
120
531
91
497
469
172
320
494
72
58
573
540
723
37
266
527
199
140
479
530
729
146
61
756
765
885
459
767
264
238
248
491
678
242
240
765
858
738
923
237
213
943
129
969
734
592
337
768
543
166
647
427
524
149
962
800
385
408
200
147
482
627
81
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