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.

968
409
600
432
414
760
194
822
830
916
89
772
981
554
229
68
489
510
835
269
707
757
466
320
257
595
503
581
538
327
40
471
299
940
606
191
481
55
771
527
733
54
163
831
683
56
164
37
319
181
984
662
599
11
883
540
79
39
161
642
903
477
898
514
82
317
831
812
588
698
581
310
46
427
621
730
659
198
688
959
799
658
320
173
796
943
976
867
26
642
539
40
538
574
760
915
109
36
235
438
BubbleSort - 0 steps
105
761
682
46
409
607
135
747
606
895
960
325
653
157
134
481
737
186
541
366
841
948
113
806
229
478
312
249
251
283
465
219
615
191
241
922
832
883
375
912
252
852
418
659
854
890
675
638
259
298
320
889
127
820
776
197
807
902
634
401
525
444
635
142
294
664
384
288
950
751
71
189
683
513
416
71
300
254
915
752
493
491
854
182
288
617
513
392
490
368
269
3
717
27
72
914
58
591
371
329
InsertionSort - 0 steps
985
493
544
464
137
116
805
414
638
470
403
581
834
155
519
119
89
417
296
409
751
10
787
10
595
308
814
539
28
963
430
782
162
196
884
468
664
999
57
769
48
10
17
930
205
782
130
824
453
839
126
871
788
35
612
951
847
513
38
566
686
315
769
333
231
722
522
42
14
117
396
336
266
476
975
813
917
631
534
690
12
631
975
588
567
694
689
878
37
735
321
368
997
57
126
997
282
415
881
736
ShellSort - 0 steps
786
988
572
734
172
805
444
773
934
520
800
408
815
911
439
282
522
138
966
175
279
66
33
561
341
947
879
672
19
17
669
188
316
837
928
152
104
269
290
599
933
599
894
257
100
702
297
812
272
381
560
35
782
228
790
318
476
34
847
3
845
373
485
665
609
39
507
628
105
964
552
587
913
141
42
906
243
411
935
422
247
993
325
527
133
853
396
705
771
457
773
251
876
844
714
464
441
96
438
369
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