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.

871
523
16
194
513
856
715
406
182
880
897
443
532
813
961
275
320
361
148
621
646
225
176
670
230
417
199
434
228
861
804
847
91
749
135
1000
717
1000
664
673
733
180
127
733
555
656
573
727
723
282
107
929
114
223
752
586
1000
779
554
980
521
633
971
320
936
774
503
299
610
188
639
442
954
216
297
851
175
764
702
964
805
24
714
676
959
217
606
164
215
18
911
487
81
325
641
683
524
304
787
389
BubbleSort - 0 steps
311
761
142
845
210
464
952
526
132
726
78
93
279
587
405
427
677
98
830
1000
855
571
912
177
579
809
586
589
664
766
737
30
54
153
994
395
700
35
217
761
95
534
739
807
867
834
283
920
198
437
567
101
286
393
571
525
628
170
122
380
611
413
218
257
506
933
13
407
830
751
703
898
375
751
359
911
339
298
269
7
482
772
802
220
685
89
206
507
39
272
549
51
942
271
51
578
147
687
687
651
InsertionSort - 0 steps
394
646
797
235
684
892
563
350
443
813
785
386
156
396
643
854
673
168
657
248
483
627
828
15
528
970
799
779
847
150
177
320
533
954
302
448
602
811
896
126
226
445
408
129
715
661
269
974
80
624
624
263
245
471
93
475
158
343
538
167
942
119
720
374
869
162
153
815
929
648
214
110
914
459
270
823
589
513
761
608
112
78
328
165
1000
52
913
275
964
784
960
299
694
562
153
747
419
953
686
288
ShellSort - 0 steps
475
191
489
987
371
482
836
549
260
405
327
503
658
569
956
524
28
9
656
811
286
817
693
432
730
107
15
740
750
858
463
711
167
716
603
580
890
104
359
421
173
423
568
574
528
883
636
860
337
620
676
137
153
411
428
228
187
463
993
903
911
196
205
71
573
183
282
767
791
102
763
304
536
412
263
220
59
24
170
889
699
943
887
102
477
50
634
803
69
158
140
355
115
870
83
69
571
137
451
307
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