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.

511
949
60
421
837
78
296
997
655
980
988
703
446
855
838
196
932
884
653
868
385
636
436
396
62
81
925
390
933
283
530
652
179
242
464
694
97
777
44
911
222
84
173
68
374
374
110
34
628
162
289
636
90
183
748
471
220
300
823
519
1000
85
586
513
77
994
53
646
838
900
12
725
971
825
587
782
948
348
407
133
132
58
743
201
924
592
621
218
927
648
200
682
381
317
268
595
494
549
199
496
BubbleSort - 0 steps
79
77
781
394
726
431
857
955
859
98
583
651
61
474
923
766
600
320
242
128
488
651
930
580
345
808
882
592
105
679
968
105
550
730
649
288
733
132
966
561
749
679
133
972
387
462
906
4
561
25
199
981
464
892
259
719
381
183
336
745
145
974
964
567
40
541
312
50
998
236
555
917
589
147
657
619
947
937
161
934
574
354
259
858
160
148
680
956
371
674
560
959
79
975
870
4
218
812
241
49
InsertionSort - 0 steps
49
402
733
186
799
1
581
38
484
804
430
272
546
153
654
880
192
93
394
917
898
467
845
172
61
696
239
758
269
774
819
636
282
801
747
273
289
651
369
5
556
940
54
540
161
977
947
519
989
314
495
662
446
13
899
896
278
926
291
768
950
689
236
560
189
127
696
376
648
72
787
232
322
504
351
516
646
741
162
856
295
78
281
353
282
322
630
433
842
318
726
33
294
122
373
4
821
595
902
681
ShellSort - 0 steps
114
942
20
843
911
612
824
232
742
31
55
146
568
846
645
165
159
225
299
279
443
643
968
959
240
709
537
508
753
244
67
262
32
771
663
204
656
866
755
172
69
99
996
541
729
895
699
236
263
422
413
639
494
386
596
545
568
884
185
214
204
305
366
164
27
589
66
588
146
188
482
908
274
785
174
634
62
944
916
536
685
878
608
956
844
394
158
558
39
344
668
181
109
78
769
73
454
349
32
832
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