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.

809
74
973
754
525
609
196
907
216
174
310
693
80
303
884
287
297
178
594
788
12
137
856
922
581
663
180
964
81
150
568
865
850
225
534
219
253
544
507
560
72
120
272
567
322
274
335
393
655
494
405
661
45
15
567
76
729
224
697
939
632
311
313
781
259
863
650
317
870
691
513
233
774
600
191
490
731
100
419
775
846
49
193
794
405
915
592
882
776
337
277
4
68
432
588
527
132
97
481
485
BubbleSort - 0 steps
670
260
868
803
236
199
11
773
473
368
195
975
808
52
831
759
523
952
197
281
950
674
260
347
389
619
474
127
808
388
157
695
663
810
13
966
140
791
724
544
921
978
780
54
828
198
445
63
188
939
201
653
821
166
398
485
765
490
478
877
246
693
819
249
698
233
407
688
180
950
350
622
696
176
836
628
45
683
701
196
294
531
918
248
521
7
161
235
194
853
768
404
221
484
592
874
899
68
309
664
InsertionSort - 0 steps
587
83
351
994
587
772
604
493
485
308
624
83
808
637
488
805
319
153
564
817
266
610
728
205
312
812
423
987
914
18
59
192
210
427
216
485
620
847
631
25
717
25
484
497
860
803
895
791
705
656
680
3
547
604
430
474
193
751
251
656
28
248
650
96
24
421
76
96
289
491
238
484
353
610
869
544
493
779
67
238
332
773
364
203
582
867
792
914
986
881
601
219
622
398
59
6
847
132
375
585
ShellSort - 0 steps
32
657
873
947
667
48
857
55
74
152
516
453
694
483
79
974
974
97
785
853
274
514
624
180
598
14
11
185
828
134
579
855
619
955
876
773
848
741
611
517
238
896
406
996
329
308
623
231
383
163
686
49
335
107
443
230
266
961
441
633
871
353
299
937
226
798
434
400
672
177
13
860
315
916
22
542
274
420
613
76
183
271
125
280
602
970
427
117
318
618
156
83
583
210
535
593
197
545
189
794
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