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.

590
635
761
758
158
443
440
450
587
754
196
842
166
806
431
621
105
209
914
660
100
696
345
925
269
589
83
154
123
946
3
94
753
567
600
803
256
120
171
717
278
999
87
234
779
930
784
135
765
34
955
507
396
185
875
913
454
360
320
428
649
925
523
990
658
411
255
13
829
349
625
479
163
43
806
46
551
174
336
231
390
665
751
536
327
57
599
326
566
170
557
490
439
258
363
598
169
84
121
528
BubbleSort - 0 steps
295
714
451
184
336
881
888
25
997
665
310
218
669
430
476
101
769
197
922
848
101
372
611
368
554
899
159
819
219
623
956
501
913
749
255
968
581
116
852
765
231
631
237
762
105
433
526
155
591
776
119
198
728
345
931
272
44
731
316
354
268
306
959
435
703
214
162
951
722
659
485
726
904
960
159
517
370
166
617
904
510
885
744
989
668
112
906
479
252
59
303
743
474
874
151
496
875
482
226
101
InsertionSort - 0 steps
90
603
847
827
988
643
146
850
285
226
88
202
238
453
228
751
346
640
605
313
539
606
606
922
264
334
266
962
136
693
804
46
181
796
595
243
50
861
149
485
445
954
430
953
696
481
464
587
281
638
848
200
906
671
131
828
450
568
767
246
703
309
402
531
661
394
650
94
201
17
282
633
3
915
413
108
908
988
752
692
372
513
200
479
385
320
151
283
54
356
59
167
901
438
568
465
108
787
759
502
ShellSort - 0 steps
165
220
401
524
296
743
785
903
263
199
389
772
659
461
898
323
737
79
516
134
801
18
251
316
652
272
878
855
124
880
486
490
781
973
669
58
998
809
887
890
764
304
568
866
10
342
396
504
166
716
933
608
869
379
734
999
937
129
626
435
19
982
167
366
505
392
295
665
383
747
774
830
830
346
755
189
28
521
711
229
456
398
372
531
219
388
45
601
390
259
878
921
544
219
323
388
936
549
905
520
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