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.

342
868
368
94
532
599
625
283
350
426
378
668
475
980
346
272
80
499
259
460
573
214
293
946
638
5
682
44
604
123
750
388
117
863
737
48
853
368
407
373
132
827
144
982
865
607
168
498
809
298
375
277
187
858
189
161
176
497
59
286
686
843
990
916
626
608
597
866
760
139
605
706
131
519
301
811
947
998
131
984
12
688
108
758
428
846
588
259
110
562
952
411
19
279
680
752
34
631
22
503
BubbleSort - 0 steps
627
468
444
174
743
717
120
570
206
93
874
265
900
749
503
905
600
965
865
597
488
821
268
347
378
480
620
4
116
31
188
494
118
653
813
489
536
526
243
922
411
872
907
196
176
293
637
763
783
79
310
200
154
551
949
245
415
821
337
691
4
93
185
44
224
150
887
720
88
623
882
609
564
915
659
946
80
642
52
851
203
73
495
109
449
264
943
88
396
345
926
309
277
168
409
539
886
916
348
585
InsertionSort - 0 steps
888
935
7
243
463
47
502
191
904
19
452
721
995
769
652
234
68
505
705
628
1
492
301
971
756
922
818
487
178
614
324
849
743
171
989
724
924
720
513
39
117
684
732
796
896
596
773
256
819
155
897
770
596
129
290
544
573
464
771
147
581
84
266
989
146
667
306
875
812
213
612
411
263
97
533
933
435
499
638
381
623
565
927
326
506
116
36
743
171
294
395
189
60
481
48
794
575
638
456
582
ShellSort - 0 steps
264
520
541
230
698
123
392
435
742
960
54
682
682
688
763
909
45
616
658
289
390
436
688
81
825
796
268
63
240
327
723
566
464
770
697
383
328
663
852
721
861
168
208
283
368
787
280
235
471
565
298
785
481
405
849
571
738
863
745
423
212
291
143
347
282
617
704
698
258
835
53
252
837
747
376
948
198
879
671
598
953
224
110
420
309
509
601
441
830
854
925
313
743
754
234
781
628
714
103
728
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