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.

66
195
199
862
802
284
882
927
899
399
203
895
392
952
332
31
204
334
380
373
70
391
65
891
332
195
888
530
677
380
174
314
165
632
914
430
855
84
487
719
277
253
71
937
266
914
384
982
585
567
516
118
683
824
696
612
434
329
851
208
207
796
971
6
254
836
249
168
626
388
913
535
971
224
313
523
46
141
724
849
250
240
33
580
559
522
535
648
706
740
847
615
977
688
93
819
249
708
120
629
BubbleSort - 0 steps
788
600
443
514
161
16
129
58
918
365
99
253
704
459
515
950
335
579
91
613
521
138
503
626
386
286
165
514
645
677
158
199
48
176
714
671
294
695
596
374
153
15
240
231
793
750
809
321
412
391
265
229
79
631
739
313
605
180
384
776
514
343
214
706
407
620
657
976
315
782
593
194
518
249
374
38
98
841
831
872
574
611
579
924
599
360
976
286
951
865
541
751
945
57
978
984
5
206
882
430
InsertionSort - 0 steps
587
745
610
217
733
364
465
535
816
453
882
104
464
301
300
908
769
995
681
781
351
573
101
795
35
440
40
589
95
666
526
820
827
157
760
631
178
862
762
204
732
561
575
969
707
597
843
89
51
616
534
934
445
853
875
935
161
823
507
630
250
957
981
579
404
929
143
901
457
230
326
29
840
594
523
750
261
595
5
960
802
970
103
851
98
978
983
168
456
321
386
873
729
627
836
604
853
261
308
137
ShellSort - 0 steps
338
66
295
84
443
897
926
154
899
783
89
516
555
355
708
352
613
701
380
97
679
648
94
892
434
493
895
479
757
263
832
546
875
543
890
956
863
71
599
800
625
282
25
782
724
807
102
924
529
882
655
947
538
447
398
883
93
668
124
903
682
977
715
236
195
840
684
937
154
832
619
804
105
88
297
559
476
688
194
191
350
441
525
644
432
483
158
390
611
344
743
900
282
309
567
220
218
963
25
897
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