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.

743
36
38
996
917
402
433
558
616
119
783
294
530
957
123
804
132
673
268
244
206
591
64
665
186
946
886
156
142
814
107
938
861
298
243
654
911
936
179
466
706
815
361
630
221
677
2
621
688
465
772
967
396
574
368
136
261
815
689
242
102
415
289
649
362
161
330
377
747
127
163
861
978
724
443
108
853
469
586
661
437
627
549
536
450
149
876
385
403
974
255
949
539
807
680
504
668
223
826
590
BubbleSort - 0 steps
2
556
182
111
166
175
368
506
169
715
909
736
416
271
708
667
744
891
970
796
924
547
700
369
315
462
841
779
434
975
272
445
775
41
688
645
112
872
942
261
256
179
652
105
503
94
630
789
2
235
543
440
826
229
606
409
90
428
766
682
231
713
304
396
610
363
902
352
724
416
65
943
549
710
173
742
981
829
908
826
723
10
704
751
889
556
20
215
438
624
624
617
171
611
207
441
346
488
914
945
InsertionSort - 0 steps
49
775
460
565
952
156
340
594
770
194
767
34
752
604
423
672
561
893
257
204
752
721
576
862
805
88
109
78
256
973
898
632
584
863
235
727
941
317
179
259
476
64
196
612
412
744
93
282
851
197
635
608
20
285
806
308
815
808
194
601
993
940
163
576
399
686
740
92
986
676
285
22
552
934
304
2
783
825
95
471
332
645
492
24
406
948
834
386
257
418
186
7
635
324
462
264
55
506
557
823
ShellSort - 0 steps
218
631
359
651
817
289
98
534
323
290
718
81
363
237
342
633
658
304
931
964
305
790
588
549
183
47
800
901
973
108
862
967
895
423
885
210
441
406
91
417
722
284
84
436
844
135
301
596
79
606
899
176
372
889
149
749
363
72
567
402
760
125
457
641
345
553
272
392
757
921
365
27
154
775
8
992
123
615
930
358
416
884
853
360
719
767
614
955
712
542
161
40
831
404
784
529
888
234
82
379
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