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.

285
559
170
379
818
797
622
786
922
2
982
67
29
184
243
755
432
25
866
165
643
698
53
952
186
587
181
488
256
755
878
752
518
455
443
738
107
633
766
281
892
615
657
507
954
187
310
666
850
880
617
107
34
258
117
388
459
967
901
624
567
432
473
599
127
65
618
436
335
749
777
289
221
442
83
153
77
623
897
459
836
676
948
752
866
316
99
423
162
379
958
839
967
308
159
916
765
57
560
572
BubbleSort - 0 steps
358
778
789
247
312
650
774
570
479
460
885
476
223
835
937
37
239
881
221
338
776
521
314
684
748
489
390
86
259
290
927
415
252
705
626
533
997
518
73
363
164
637
154
107
678
995
509
237
445
296
612
985
840
862
712
335
336
598
653
840
683
546
990
148
298
295
473
273
482
603
161
493
299
944
705
159
60
846
181
21
911
915
894
315
479
646
360
517
884
553
342
844
617
372
486
947
282
189
292
97
InsertionSort - 0 steps
732
571
41
205
762
305
95
622
904
98
487
235
318
369
618
108
90
521
340
608
79
473
545
574
916
926
49
898
877
888
580
805
287
552
629
804
71
607
68
853
534
778
160
48
843
512
217
316
228
484
516
832
336
78
823
561
373
229
661
808
813
150
966
432
703
160
215
80
186
971
895
445
199
91
440
430
37
99
503
366
544
402
36
292
733
329
681
410
255
148
108
947
615
948
802
19
581
194
699
237
ShellSort - 0 steps
63
763
987
276
519
81
419
488
1
715
555
537
736
465
538
460
538
475
353
144
738
745
887
659
953
895
82
476
974
558
150
804
802
912
310
28
438
929
171
114
762
204
120
548
449
576
797
918
571
962
915
977
119
570
593
77
820
172
97
925
612
822
710
713
479
542
328
369
759
14
145
684
402
751
232
792
799
47
113
770
196
481
729
717
532
667
569
610
475
805
706
123
116
822
695
933
772
808
878
789
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