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.

783
773
171
201
186
803
857
303
84
144
442
708
229
595
897
176
378
783
784
191
4
471
858
761
343
212
126
184
310
222
774
479
681
404
932
400
409
550
884
417
907
770
334
815
690
872
381
460
432
586
180
872
283
359
959
778
377
925
873
650
837
942
936
262
62
621
516
89
888
827
922
276
275
238
685
606
440
966
559
54
691
193
781
827
783
956
14
135
403
76
688
138
240
48
122
88
465
461
991
314
BubbleSort - 0 steps
53
216
854
559
867
126
768
306
859
47
876
620
816
350
757
473
40
21
396
827
962
542
144
596
489
3
680
762
624
256
4
181
522
916
782
177
951
773
951
809
291
56
729
58
134
375
732
779
3
947
294
72
567
784
131
539
347
307
350
429
22
155
553
449
273
742
963
674
600
829
403
315
104
540
737
120
862
401
621
745
238
153
658
858
615
832
995
53
232
614
879
228
760
965
909
897
400
927
256
953
InsertionSort - 0 steps
634
30
461
956
870
99
464
724
35
727
710
119
53
628
64
369
113
418
727
48
431
325
489
45
930
827
805
320
760
384
70
822
251
329
174
425
623
57
524
905
71
524
613
636
708
654
768
648
622
186
868
625
709
869
501
137
133
452
295
154
147
15
376
573
307
361
568
799
136
820
867
655
100
103
249
94
721
507
842
300
382
449
488
728
216
660
110
463
564
404
243
707
323
438
476
547
617
900
537
180
ShellSort - 0 steps
979
305
430
215
421
166
41
585
531
102
68
733
294
763
122
74
965
877
886
458
467
93
452
752
65
8
396
590
162
172
524
977
201
642
335
445
571
131
968
170
433
788
711
241
971
837
611
378
593
10
98
537
442
327
956
718
743
208
545
238
947
444
242
338
129
593
473
822
600
314
801
28
91
213
669
211
631
205
344
839
968
807
810
451
963
324
704
795
906
291
572
387
587
212
297
951
46
525
684
582
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