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.

909
127
799
739
55
617
921
132
271
551
682
572
31
440
243
47
221
615
350
802
923
495
157
317
682
767
273
534
503
798
252
48
478
149
727
141
975
848
288
386
553
988
420
240
556
513
574
258
948
145
871
406
54
317
276
165
768
977
325
92
305
313
559
780
116
6
969
115
175
72
143
675
333
128
740
606
583
781
740
937
645
718
145
3
574
89
182
471
957
721
416
141
427
857
338
967
75
94
751
952
BubbleSort - 0 steps
551
171
56
622
399
388
176
901
774
340
218
320
945
355
200
55
911
917
939
779
321
851
349
267
423
958
596
294
173
771
965
768
427
459
856
808
606
845
152
895
941
861
997
10
603
414
589
329
578
278
571
486
74
824
701
503
247
278
226
514
812
696
575
145
493
2
736
342
108
81
677
653
894
646
334
888
59
933
431
52
14
529
126
532
167
664
283
567
585
663
264
607
126
282
747
884
116
166
377
703
InsertionSort - 0 steps
419
394
666
759
677
994
367
479
465
549
52
127
690
199
768
155
812
986
336
476
287
12
708
317
926
175
330
965
133
465
61
886
75
505
553
178
786
283
256
431
472
702
471
188
748
348
444
822
706
838
889
744
416
928
805
966
79
919
176
4
581
31
326
575
138
627
83
389
752
883
151
102
739
123
142
734
764
915
252
124
882
939
244
879
317
555
662
459
395
692
620
440
421
682
828
456
132
355
933
93
ShellSort - 0 steps
267
227
218
931
397
101
972
800
900
316
519
919
418
66
975
418
592
773
212
50
213
969
135
412
392
175
182
108
742
408
392
424
834
160
431
677
45
133
559
887
827
326
184
3
537
448
763
263
820
435
895
432
74
301
7
68
477
652
523
271
917
844
999
994
468
202
656
118
103
694
631
912
827
223
96
426
743
832
257
534
463
929
495
221
44
674
300
813
970
145
549
66
47
578
117
433
70
407
795
733
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