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.

589
610
464
714
881
632
967
591
325
675
727
372
372
761
723
112
955
918
1
645
542
359
709
479
905
847
714
680
827
799
594
881
347
373
149
713
359
789
667
226
830
351
865
295
995
873
37
807
695
17
699
876
32
758
764
181
338
116
145
605
850
226
854
818
548
699
390
40
557
928
519
628
316
414
181
451
93
725
686
856
485
872
211
795
235
21
979
159
435
230
989
181
244
344
455
864
676
50
891
846
BubbleSort - 0 steps
508
624
402
481
217
598
984
536
316
179
324
53
900
240
546
45
51
445
953
254
155
186
536
470
524
816
765
247
887
144
756
988
844
84
109
234
558
343
370
109
246
112
584
760
189
501
342
558
500
320
23
365
572
607
738
235
115
530
24
582
391
810
359
615
294
927
356
577
197
418
982
67
660
957
91
272
291
777
779
483
130
406
962
384
259
869
60
170
118
126
934
273
897
851
405
150
601
838
630
363
InsertionSort - 0 steps
344
10
777
511
617
182
126
649
275
4
688
470
245
905
672
642
730
632
576
319
509
305
942
337
675
759
80
25
976
464
714
290
677
366
315
466
581
336
617
235
479
95
153
480
682
690
106
204
52
588
466
317
871
111
128
361
110
663
256
344
538
843
425
896
280
749
156
163
70
282
862
655
835
4
22
643
663
99
577
289
170
716
115
3
195
283
860
2
749
439
114
859
501
924
491
306
975
821
74
52
ShellSort - 0 steps
87
543
310
971
647
119
671
929
836
788
719
955
266
923
270
219
993
86
823
824
623
306
927
204
381
930
833
115
153
273
684
647
935
129
273
420
60
248
96
711
671
888
402
824
94
388
959
638
837
967
967
706
942
714
930
29
583
493
290
73
78
236
627
113
676
928
805
531
672
795
329
352
99
738
579
584
390
517
135
917
691
910
537
510
627
690
824
627
651
655
94
417
767
643
337
951
215
711
145
559
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