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.

181
164
928
322
750
994
60
415
994
208
418
853
783
918
611
192
950
847
950
426
849
395
943
965
259
792
281
684
669
848
308
537
223
662
847
965
631
644
278
865
446
147
103
514
264
312
440
375
980
926
710
235
913
990
728
406
558
240
325
164
366
500
536
999
370
143
126
677
482
119
117
263
122
978
762
333
847
731
127
257
608
794
466
936
217
70
963
77
784
110
271
6
28
838
736
344
619
446
704
84
BubbleSort - 0 steps
431
867
175
405
319
633
960
385
535
454
642
426
491
481
46
508
900
378
771
253
193
478
70
924
817
979
87
290
184
952
356
603
754
466
345
78
466
658
292
407
36
440
805
126
930
163
175
679
948
422
93
755
75
603
339
957
251
565
310
650
499
71
431
95
872
944
66
629
733
475
1000
449
277
786
466
515
713
582
465
631
556
798
869
568
225
828
941
739
579
492
887
874
888
57
89
417
599
513
422
220
InsertionSort - 0 steps
522
917
826
514
942
76
794
254
916
464
209
461
741
397
281
605
35
71
921
1000
884
161
957
919
333
600
407
856
993
262
408
406
417
940
384
254
953
159
802
578
571
185
991
265
739
943
466
527
467
486
552
827
785
738
974
980
276
629
982
351
646
873
948
698
176
591
50
947
648
744
171
755
427
55
663
378
673
687
366
6
175
974
346
219
807
461
320
107
976
394
486
886
15
570
772
274
21
66
168
183
ShellSort - 0 steps
481
374
735
713
637
934
669
495
815
360
825
740
347
279
997
534
458
578
754
506
611
726
557
363
217
630
348
845
560
279
101
807
868
840
364
704
85
341
638
591
79
599
551
372
770
726
289
138
39
646
98
227
516
220
859
990
161
826
42
643
244
753
523
435
720
272
278
795
632
978
406
315
935
807
47
937
60
960
641
941
917
428
320
164
134
892
175
208
296
983
278
900
495
867
322
622
235
554
517
749
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