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.

352
158
917
798
820
459
40
1
187
64
856
605
989
229
812
384
855
916
1000
464
813
351
788
309
133
793
74
978
996
764
929
835
45
750
942
766
377
574
52
378
165
597
161
280
376
989
41
899
83
355
335
468
916
579
364
923
516
838
698
150
74
245
435
612
411
679
61
969
392
353
510
581
403
993
24
523
338
670
726
258
97
266
968
562
948
523
703
945
921
620
866
533
113
243
890
347
609
894
294
372
BubbleSort - 0 steps
855
168
267
465
790
220
616
269
480
718
964
491
889
411
78
374
81
411
727
40
760
847
617
451
77
531
821
764
761
107
467
384
503
763
833
267
375
138
274
991
371
890
312
351
83
628
226
119
80
731
38
101
911
115
558
222
576
542
33
684
622
12
670
715
985
985
975
517
527
43
102
751
361
948
947
302
755
395
414
694
229
192
477
88
445
506
302
830
609
847
39
141
808
672
8
581
317
690
976
750
InsertionSort - 0 steps
883
541
845
575
508
597
399
657
850
385
115
67
291
313
716
669
218
562
99
854
410
479
744
816
467
517
69
1000
370
23
505
55
471
11
970
81
706
162
283
83
47
853
579
653
106
863
225
253
49
475
166
840
452
309
91
299
453
589
363
581
974
139
300
374
473
935
59
134
419
889
972
381
659
719
1
828
880
523
698
644
226
540
180
227
125
979
300
930
672
977
768
862
447
672
264
422
179
767
860
324
ShellSort - 0 steps
171
108
923
819
427
253
187
356
248
491
355
764
755
72
114
569
274
311
754
551
597
310
286
150
203
476
295
75
185
422
35
238
53
470
512
28
100
25
632
710
138
885
857
722
998
759
453
453
344
170
946
995
868
374
216
683
339
357
125
270
97
285
424
450
511
651
860
559
691
57
510
497
256
980
100
690
220
242
992
698
777
642
747
225
169
381
928
515
718
92
395
136
584
465
172
463
913
153
196
940
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