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.

421
274
71
568
474
259
288
110
487
719
482
332
788
495
505
545
375
746
160
428
601
831
96
46
876
475
858
816
850
179
505
736
184
718
816
608
504
813
195
163
798
148
667
319
884
579
770
182
337
451
661
562
136
354
690
29
544
642
275
895
504
154
139
259
146
241
841
257
331
637
743
389
239
939
428
726
249
197
616
201
288
833
160
733
551
214
664
744
286
48
121
119
723
286
324
728
348
680
86
511
BubbleSort - 0 steps
59
931
127
67
616
278
229
78
627
347
462
9
961
895
723
986
396
902
980
859
335
122
496
478
138
645
394
458
940
206
807
235
622
392
624
941
571
465
33
837
506
470
1000
618
475
848
906
815
845
878
669
104
190
367
169
428
421
837
303
354
432
815
621
927
598
43
314
809
171
851
515
884
61
118
656
516
40
102
98
223
399
353
586
677
784
457
973
431
46
302
475
220
584
910
553
856
736
44
691
48
InsertionSort - 0 steps
390
810
941
683
741
687
198
449
780
486
529
542
155
530
507
592
495
366
5
234
363
909
251
207
768
90
948
628
551
781
10
154
497
3
206
974
879
107
563
983
519
336
916
501
239
566
288
997
562
886
625
363
853
675
417
625
332
974
551
476
57
892
794
838
381
106
397
128
407
352
963
97
82
945
341
390
943
461
854
168
875
931
37
818
955
643
595
287
576
546
924
776
502
359
775
285
410
763
126
365
ShellSort - 0 steps
176
592
719
410
752
412
209
758
818
601
378
332
313
762
274
943
737
556
446
772
535
538
500
891
909
59
536
963
778
814
614
999
346
486
225
179
975
686
869
490
930
899
990
338
374
293
952
93
184
461
202
581
139
353
1000
656
736
220
290
666
381
641
386
758
900
443
164
290
697
762
292
203
279
197
954
812
949
753
155
599
751
508
768
548
439
63
173
779
137
714
813
486
379
286
21
258
770
962
197
132
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