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.

841
489
327
613
462
821
371
253
258
259
736
443
602
426
602
8
308
584
405
241
945
571
535
720
18
439
323
743
94
848
331
296
205
602
197
372
593
169
110
590
786
336
282
310
697
956
34
113
246
798
845
568
974
403
617
752
69
515
362
72
321
594
838
965
569
539
869
505
136
536
268
807
373
877
96
819
789
40
791
400
617
22
849
375
332
632
360
444
20
250
38
700
737
867
763
755
442
930
655
421
BubbleSort - 0 steps
371
892
178
889
424
642
396
533
733
561
829
966
441
766
837
620
669
260
983
194
170
495
947
630
167
460
539
613
786
335
134
368
245
541
633
668
861
659
311
749
172
804
6
841
404
896
934
219
717
135
929
411
628
923
203
605
411
200
664
602
828
183
240
802
546
102
253
913
798
300
275
216
325
320
286
410
227
186
398
428
336
425
639
820
368
303
555
679
723
549
501
998
694
47
832
881
468
100
292
377
InsertionSort - 0 steps
697
51
827
418
807
810
654
97
623
38
300
70
527
42
443
67
30
532
292
289
1
892
119
905
45
571
136
601
657
661
203
79
167
213
475
531
777
583
878
628
768
759
645
572
951
819
9
725
574
230
168
231
98
286
235
204
705
91
820
131
35
237
287
963
329
80
266
829
816
638
32
342
476
36
963
814
228
207
320
326
96
214
709
31
17
992
574
645
501
985
576
481
239
975
817
991
646
555
472
642
ShellSort - 0 steps
505
278
724
280
149
569
447
814
770
872
872
137
970
900
910
250
426
130
617
391
586
903
52
836
895
409
135
394
585
472
926
444
661
845
48
312
969
195
751
120
539
523
84
133
589
296
144
42
837
187
296
612
747
865
130
245
362
87
493
294
488
774
538
259
829
484
591
296
752
661
862
720
918
733
332
938
480
626
380
191
158
764
354
304
433
731
198
59
14
326
67
95
339
486
510
67
639
592
151
846
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