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.

468
106
533
167
289
303
616
116
374
753
326
75
153
86
978
10
240
627
457
134
707
521
700
697
140
390
886
513
220
576
668
233
989
783
699
417
934
993
602
765
512
691
665
299
834
505
989
235
162
49
755
289
983
610
35
339
13
859
178
210
469
62
684
72
270
126
32
189
625
657
530
57
838
402
522
913
262
409
624
254
989
619
949
441
612
673
230
10
975
836
844
997
74
753
959
797
785
644
340
157
BubbleSort - 0 steps
141
455
984
711
984
429
652
704
309
301
48
676
805
741
349
620
566
122
424
598
787
629
532
504
740
902
195
654
185
296
131
48
647
437
27
822
740
819
433
367
64
776
637
11
640
151
767
529
210
682
625
370
204
119
168
144
867
384
503
978
529
731
808
344
620
182
843
24
206
833
102
482
289
910
697
703
813
119
529
597
132
772
534
156
343
487
303
797
736
840
675
189
67
977
587
908
357
228
703
421
InsertionSort - 0 steps
642
476
731
674
449
384
350
38
341
926
669
137
746
979
634
17
286
885
893
298
835
743
802
219
668
395
147
717
918
612
152
271
800
34
851
569
330
496
196
788
253
500
293
991
911
377
859
521
483
464
784
370
638
312
55
886
232
998
572
707
144
487
368
954
226
647
655
84
282
456
80
256
781
742
599
469
320
813
677
837
612
309
835
579
604
122
951
532
192
804
812
281
730
350
262
587
801
732
672
160
ShellSort - 0 steps
133
726
913
437
373
984
879
79
284
2
263
301
605
8
778
972
325
828
658
371
807
688
875
199
257
178
77
885
617
156
695
505
839
394
861
501
310
22
9
134
70
154
348
73
70
993
37
880
834
633
971
325
121
659
150
229
946
677
252
918
214
944
419
251
593
297
698
508
990
533
961
685
878
204
590
444
711
439
109
58
215
209
552
429
256
132
464
736
891
534
930
48
66
220
378
7
659
218
840
856
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