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.

751
574
701
605
418
418
223
912
205
306
179
427
507
419
883
601
828
769
450
236
995
956
275
855
69
206
708
823
801
806
972
409
207
907
502
844
768
572
840
370
730
69
22
950
939
18
405
383
391
999
329
51
508
775
501
559
344
989
643
119
798
868
53
379
60
934
787
47
234
640
36
123
4
340
337
961
951
514
759
285
23
212
153
486
3
547
35
770
955
886
560
417
186
881
582
21
413
376
436
47
BubbleSort - 0 steps
330
978
496
802
507
132
135
111
41
572
101
936
714
525
774
122
967
632
88
689
715
538
251
604
160
705
921
391
671
427
165
312
251
126
689
828
523
627
330
412
392
537
774
740
226
733
148
773
83
668
417
556
668
840
71
153
230
557
24
538
481
531
750
512
770
18
235
735
698
975
481
829
44
942
192
119
890
41
391
824
604
134
902
201
546
156
307
131
41
466
23
353
113
691
451
68
393
181
868
723
InsertionSort - 0 steps
989
300
746
977
794
731
353
224
99
606
512
837
955
216
805
599
329
821
208
223
30
995
554
511
575
160
371
716
185
24
301
994
825
462
402
982
67
231
114
427
562
649
168
81
741
76
868
157
508
645
168
666
171
180
474
729
700
277
105
342
983
35
29
902
302
654
1000
276
987
409
576
359
742
858
468
215
697
165
318
599
963
864
668
561
370
657
490
953
787
622
649
295
733
249
715
645
129
279
319
358
ShellSort - 0 steps
393
901
851
201
728
944
496
803
106
245
155
332
216
289
951
275
435
778
655
439
789
192
156
827
129
936
980
671
14
501
322
110
299
465
670
876
472
654
257
439
388
995
720
71
533
735
645
964
751
777
655
582
513
776
863
203
441
555
968
347
677
388
488
463
146
750
755
936
813
38
791
463
411
492
298
950
917
456
309
640
394
217
34
69
208
936
591
945
728
708
690
466
509
735
160
198
871
913
986
510
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