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.

676
233
382
820
361
28
606
674
792
15
808
640
184
403
101
393
145
786
83
716
448
352
32
412
12
930
52
466
618
390
95
680
565
203
392
953
725
519
971
610
682
201
420
84
46
862
463
231
767
582
663
445
239
914
744
522
570
585
289
56
503
658
276
669
471
52
251
335
52
721
200
575
519
967
241
458
336
494
25
454
769
135
616
548
353
826
859
232
114
998
712
690
535
903
269
807
956
659
655
307
BubbleSort - 0 steps
992
419
538
590
619
483
62
985
911
195
813
729
898
500
851
78
158
845
414
100
45
961
256
733
660
701
22
976
415
80
133
290
245
105
771
385
862
584
940
378
656
864
963
355
863
940
511
612
403
493
126
274
920
558
149
926
904
960
771
978
612
624
360
488
430
222
540
929
349
390
713
136
550
898
188
319
522
335
990
383
196
445
326
358
720
316
714
163
982
951
48
389
2
163
378
568
840
335
841
613
InsertionSort - 0 steps
142
723
422
579
773
880
476
589
232
456
676
479
704
442
847
512
122
748
710
865
944
727
306
902
323
90
307
503
660
482
243
353
351
317
493
919
191
817
212
556
549
537
753
595
688
864
498
242
435
287
900
87
33
42
891
351
843
81
614
58
604
758
140
198
789
142
668
39
548
555
879
474
814
58
226
929
412
954
26
30
723
888
632
413
338
878
164
749
822
227
278
163
868
81
291
329
42
237
408
872
ShellSort - 0 steps
745
103
912
307
689
500
424
779
527
549
30
711
164
364
381
554
922
106
702
339
958
796
964
305
664
394
712
970
371
55
812
619
623
303
71
346
140
938
964
144
836
292
775
706
91
246
53
52
94
114
796
984
799
958
46
336
157
684
69
558
563
424
630
550
74
805
899
42
845
739
175
142
168
595
623
714
348
492
622
397
848
781
398
970
752
622
502
565
67
761
595
558
837
51
72
870
198
479
655
699
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