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.

24
972
677
285
427
90
803
393
672
783
530
894
100
142
187
981
362
505
596
709
521
125
597
965
39
85
713
771
426
796
322
82
513
917
934
782
234
901
167
202
129
283
361
959
340
858
886
928
358
277
127
614
720
93
851
831
74
836
589
625
874
733
760
499
994
695
442
238
789
849
258
867
59
973
485
644
392
263
212
982
309
426
556
964
786
97
915
392
923
638
703
88
508
357
663
949
844
872
509
308
BubbleSort - 0 steps
587
322
651
46
531
783
649
988
340
211
32
206
922
976
579
124
783
274
456
852
57
44
140
1
391
711
54
559
834
779
826
431
11
356
250
536
527
585
537
950
876
739
319
970
826
700
910
872
992
301
465
105
324
803
925
905
341
486
308
13
446
713
11
582
618
80
771
625
867
984
948
616
290
940
307
726
520
975
115
705
841
792
417
154
63
925
714
589
896
546
309
269
692
228
785
988
495
39
397
815
InsertionSort - 0 steps
419
979
206
24
187
119
252
965
523
109
948
533
110
44
141
499
70
776
247
463
989
930
900
266
381
488
49
495
735
132
618
991
151
250
364
291
339
948
962
845
612
762
838
500
523
956
604
286
164
50
661
322
813
420
524
219
623
559
867
677
755
238
141
290
650
843
317
609
904
7
758
287
56
413
862
881
428
482
876
959
782
871
58
9
279
672
302
286
752
459
349
370
676
985
177
871
172
110
664
594
ShellSort - 0 steps
7
179
99
897
97
333
109
644
164
196
223
769
209
478
210
47
333
483
967
325
146
554
896
493
257
213
6
48
837
417
831
762
129
156
375
25
459
86
698
51
799
17
668
363
324
33
839
535
545
36
138
865
582
26
259
612
596
142
935
645
160
939
458
888
133
692
513
562
225
332
521
285
767
781
112
118
33
25
32
329
606
95
70
858
199
902
375
850
536
569
935
835
284
896
118
553
282
956
312
178
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