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.

890
390
207
353
621
559
67
218
700
114
413
395
101
523
610
456
979
901
620
616
785
597
107
447
526
872
583
278
881
733
620
255
42
868
725
806
409
720
370
994
833
83
414
257
955
62
41
172
700
965
876
716
714
402
9
513
497
303
661
775
96
749
577
442
728
521
899
905
517
180
949
282
269
691
133
476
15
253
432
553
924
964
926
787
120
34
122
403
393
594
49
30
295
636
432
307
379
451
376
969
BubbleSort - 0 steps
234
848
168
541
33
25
741
584
95
552
757
99
821
890
194
36
864
56
545
362
887
595
60
801
500
485
214
689
467
107
353
145
214
802
546
916
271
488
115
608
349
102
843
203
957
979
268
692
879
8
423
608
524
973
206
723
536
106
80
31
800
205
539
451
145
666
230
19
225
152
578
601
118
251
6
21
22
426
870
958
640
428
265
156
494
574
157
226
82
733
147
369
527
657
895
779
829
842
75
583
InsertionSort - 0 steps
312
557
172
725
154
368
579
667
319
330
785
180
660
472
419
747
424
428
397
292
341
650
159
574
58
652
878
430
385
667
997
557
843
2
724
56
296
99
940
286
255
245
977
426
848
680
385
470
802
467
541
968
29
459
497
1
302
582
854
634
61
554
592
414
637
824
760
490
282
519
131
334
318
641
1
290
257
744
183
178
170
425
79
125
173
278
200
475
91
100
168
183
399
974
123
9
4
989
675
118
ShellSort - 0 steps
172
601
642
214
488
587
733
714
747
45
762
709
672
352
905
547
921
757
943
610
376
734
4
847
125
39
236
842
22
493
886
686
167
909
210
84
772
188
395
747
434
397
804
773
235
635
637
21
865
388
145
386
564
170
431
479
775
558
307
225
656
883
649
62
748
363
890
157
255
417
702
559
187
357
891
246
887
794
614
433
154
616
829
22
582
770
352
231
142
486
512
234
807
119
382
70
348
334
584
62
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