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.

885
869
792
974
736
10
741
895
791
317
291
397
610
256
681
310
456
156
447
291
317
668
730
289
190
916
563
722
829
947
394
456
480
343
122
458
635
574
723
209
768
446
216
208
227
134
866
481
433
32
705
212
10
184
391
985
179
282
978
469
769
676
330
837
702
853
52
134
346
451
630
168
208
254
731
450
579
360
600
963
815
608
184
890
554
827
19
384
427
666
698
531
126
314
955
385
179
982
462
641
BubbleSort - 0 steps
188
167
291
97
866
224
997
171
602
448
186
618
801
429
802
696
842
270
341
235
42
544
933
954
344
769
81
61
949
827
127
785
121
127
569
39
982
63
599
141
661
970
729
601
809
973
483
924
617
933
429
240
237
656
153
974
849
724
217
111
734
729
85
687
929
807
931
916
470
612
609
924
699
81
646
663
960
280
739
723
688
632
304
877
901
877
644
698
354
695
703
925
553
263
311
47
684
727
72
711
InsertionSort - 0 steps
723
109
148
500
818
691
382
676
694
74
785
355
725
145
304
296
508
876
187
658
480
30
806
400
946
678
145
659
614
2
590
464
863
73
244
420
985
280
42
849
741
37
539
887
72
483
249
775
389
966
476
930
894
368
634
911
673
392
825
881
343
536
717
801
335
758
340
107
684
374
245
536
121
217
559
751
893
366
169
758
481
345
713
217
725
287
666
272
493
470
842
180
914
285
1000
970
428
363
627
679
ShellSort - 0 steps
819
147
443
57
200
839
201
746
376
75
776
866
846
820
687
319
433
767
251
585
884
2
872
242
256
654
667
910
370
994
420
784
873
871
27
762
237
370
67
259
760
810
999
688
505
869
184
275
719
384
4
48
350
609
540
331
595
370
624
670
838
182
253
30
772
394
912
568
129
53
147
41
426
855
251
162
790
758
462
18
959
55
92
891
141
478
936
810
347
181
230
918
537
861
134
879
320
91
980
429
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