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.

806
422
568
855
996
775
494
483
155
310
447
645
616
505
857
474
181
755
580
742
492
886
639
755
852
117
359
480
602
750
87
230
706
995
822
285
382
87
343
731
635
84
602
503
833
516
600
547
940
621
600
102
551
760
998
734
912
196
12
97
597
207
299
720
160
149
815
974
841
212
881
622
500
660
327
275
242
743
430
474
731
125
406
262
103
64
725
268
792
778
205
694
852
26
217
370
635
45
607
670
BubbleSort - 0 steps
939
98
563
881
316
283
419
172
318
339
351
461
147
799
874
864
857
253
581
559
482
188
631
910
957
240
228
102
915
401
750
239
481
635
44
214
57
636
502
512
93
548
283
776
705
344
455
632
722
118
982
663
938
615
722
692
150
5
728
794
558
782
406
669
73
897
257
691
210
504
513
226
623
706
480
505
915
89
552
733
49
4
474
304
945
620
907
803
6
870
584
344
834
646
288
918
741
15
89
691
InsertionSort - 0 steps
165
769
11
87
412
918
893
119
913
110
243
180
959
109
238
716
257
142
724
407
530
885
276
549
509
284
688
1
816
485
346
205
336
721
898
948
108
310
23
524
302
550
914
619
758
760
62
64
553
99
531
991
236
668
211
524
311
966
16
752
227
713
628
332
67
579
720
797
353
160
697
407
686
249
154
552
818
965
785
995
738
858
770
419
655
470
569
815
656
562
395
962
839
663
37
743
870
84
174
909
ShellSort - 0 steps
184
161
562
701
723
309
337
947
663
339
411
352
38
843
52
452
450
519
901
204
792
719
895
100
761
328
206
274
384
937
769
549
639
945
493
964
30
984
796
505
117
247
808
552
524
18
635
817
140
128
852
361
171
922
264
889
617
240
383
763
121
838
686
152
423
436
142
777
12
343
968
717
848
393
11
400
798
114
742
865
394
563
380
836
806
137
829
679
371
755
205
701
605
687
609
320
794
528
840
641
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