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.

281
849
747
10
938
38
640
515
328
979
607
410
144
435
550
497
590
416
429
114
723
955
41
789
567
255
68
200
776
663
338
174
663
375
184
330
549
87
988
67
710
172
787
134
917
718
861
481
775
424
593
186
204
513
833
547
106
549
619
595
917
694
126
432
112
88
235
556
545
541
256
75
119
277
704
407
885
398
132
790
518
61
328
594
427
809
160
196
942
129
73
123
883
727
882
528
238
694
441
499
BubbleSort - 0 steps
799
732
650
274
437
723
757
500
163
75
274
31
629
132
874
926
2
125
180
323
824
26
501
402
348
745
964
567
487
780
160
739
807
581
208
910
776
864
754
84
731
180
319
625
655
524
627
123
840
508
654
63
354
724
648
932
154
394
145
612
615
256
782
657
160
804
115
93
819
920
963
590
494
325
497
661
943
183
826
407
350
51
181
428
137
574
614
545
733
827
547
219
98
520
849
540
206
393
2
353
InsertionSort - 0 steps
68
164
624
467
62
907
488
983
499
653
14
210
849
642
955
770
973
497
104
580
481
998
103
79
948
898
694
73
494
344
952
393
832
445
294
619
661
83
112
638
140
35
763
159
453
63
550
439
449
866
410
789
803
979
387
321
819
651
512
948
753
355
84
26
143
442
65
520
496
178
276
888
924
702
208
383
178
545
500
694
522
75
784
115
390
529
993
35
595
429
926
96
79
346
483
428
301
508
654
457
ShellSort - 0 steps
451
970
825
439
762
577
144
471
173
90
938
646
368
281
883
194
467
521
880
996
214
543
230
244
285
880
891
224
812
619
644
327
460
854
193
905
662
790
98
749
674
200
577
794
460
630
5
838
609
899
721
430
700
836
53
216
414
477
288
127
985
862
69
147
613
475
895
442
232
79
841
599
874
817
615
342
826
318
304
536
421
811
266
82
176
399
844
5
193
46
713
596
950
820
857
238
665
856
537
210
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