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.

714
444
775
904
996
42
958
55
248
934
422
37
881
888
683
945
648
884
337
109
894
752
348
785
573
483
297
660
173
848
854
487
270
500
485
460
768
776
923
499
338
808
88
290
209
886
758
958
882
128
379
530
148
433
235
173
156
484
368
219
31
364
35
649
166
918
155
349
638
653
379
858
423
805
447
398
109
644
869
604
41
932
390
959
578
108
806
601
962
343
194
736
228
771
40
407
681
493
294
529
BubbleSort - 0 steps
584
48
372
818
924
488
762
266
165
258
255
858
999
725
292
415
265
964
471
24
346
987
988
693
564
631
181
676
686
739
232
853
765
998
69
863
296
122
346
847
787
427
229
496
914
328
281
690
890
631
842
610
760
992
997
91
537
327
273
848
454
50
355
793
635
943
3
718
858
105
921
451
9
827
841
768
959
707
687
519
945
536
926
592
539
503
601
724
31
749
350
149
62
549
537
395
780
799
126
794
InsertionSort - 0 steps
469
500
223
222
844
665
446
848
871
324
984
355
980
838
840
661
482
128
423
192
730
738
62
755
227
241
174
636
714
799
954
159
725
228
597
865
406
698
476
422
969
643
139
662
284
847
136
651
277
620
813
143
474
681
619
461
956
899
922
27
935
158
294
721
189
109
214
804
737
580
10
831
562
822
162
388
135
909
721
291
403
615
165
930
864
716
986
734
519
341
194
81
210
271
921
419
792
624
892
30
ShellSort - 0 steps
874
85
78
632
61
654
160
620
7
937
351
812
638
608
234
343
649
360
515
870
959
10
137
363
986
141
860
693
363
650
864
555
787
201
517
871
145
432
97
420
558
188
187
732
953
779
160
996
81
542
318
357
399
110
928
566
378
411
234
257
754
669
269
725
119
355
736
378
39
467
490
753
931
423
170
375
999
371
962
113
887
81
828
501
833
831
273
703
293
381
924
893
319
346
340
356
677
634
575
221
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