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.

639
165
970
767
415
578
795
826
44
83
828
966
852
972
706
541
846
902
555
468
741
964
372
364
133
670
963
36
256
947
699
429
124
707
537
728
744
880
671
198
480
32
868
403
488
96
396
124
505
503
35
566
789
765
782
958
902
669
170
143
601
243
909
19
543
813
307
313
637
514
668
20
1000
110
848
868
404
619
387
476
48
982
941
468
451
243
697
147
635
983
569
359
36
152
109
576
299
120
606
16
BubbleSort - 0 steps
916
865
686
757
808
946
176
815
624
857
993
59
756
614
121
345
136
778
961
560
676
859
105
623
48
151
690
738
100
125
962
592
805
502
400
413
693
861
963
134
559
474
36
146
33
67
78
256
780
126
727
337
327
699
442
14
36
949
921
626
589
149
460
115
202
494
336
142
164
949
451
279
872
927
34
689
804
953
197
786
940
352
57
517
300
864
319
552
605
949
250
626
605
321
872
14
258
467
960
461
InsertionSort - 0 steps
563
485
111
218
662
47
178
873
979
546
620
829
408
730
196
648
6
338
563
895
458
726
698
751
50
30
780
898
745
487
362
982
936
367
851
835
645
842
191
964
324
841
607
749
987
754
560
108
656
477
499
410
58
320
703
897
534
407
81
284
18
696
322
222
498
841
574
535
734
469
987
350
846
243
96
835
415
872
318
806
633
740
596
935
388
455
60
16
870
263
939
260
311
757
862
78
586
784
319
245
ShellSort - 0 steps
160
157
899
563
569
503
589
38
163
782
391
516
264
719
484
794
970
654
1
215
300
480
463
590
653
901
281
64
334
40
347
216
117
209
745
647
740
286
575
861
97
281
297
482
46
765
630
509
485
160
754
419
30
36
754
384
659
329
468
150
109
613
664
964
61
768
264
615
34
766
621
306
974
379
891
548
453
281
271
308
847
374
223
835
723
673
68
830
215
633
413
561
808
518
287
565
499
348
846
730
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