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.

291
784
857
446
428
374
132
66
382
934
772
558
10
285
730
907
58
959
858
929
577
389
341
392
20
809
330
503
86
472
963
561
768
65
414
743
159
622
318
946
793
144
66
904
41
668
418
445
623
579
857
524
523
143
428
683
391
890
589
442
199
824
421
858
453
861
563
26
972
492
631
776
500
473
903
751
197
929
377
223
146
698
312
202
774
581
74
603
209
761
269
585
182
14
658
552
635
652
445
56
BubbleSort - 0 steps
885
940
929
258
320
912
893
460
201
929
663
695
502
765
743
782
41
98
400
246
9
287
442
198
150
504
29
646
742
402
643
909
91
464
445
160
556
185
840
189
484
23
1
840
253
895
860
934
858
526
373
834
568
367
206
813
12
61
982
303
421
788
166
33
383
846
592
612
151
253
371
673
219
61
294
166
628
411
529
128
113
480
803
274
52
736
790
20
95
801
895
664
717
951
620
780
276
484
726
377
InsertionSort - 0 steps
996
496
235
426
1000
116
430
630
28
436
743
97
843
658
542
417
399
48
577
780
489
25
234
596
913
55
433
345
821
399
267
928
3
440
677
760
714
326
174
64
760
120
885
210
532
93
561
492
838
118
826
671
496
357
932
151
516
716
668
744
717
673
865
138
422
923
688
966
662
704
597
224
6
174
976
614
418
30
236
498
746
591
244
498
475
795
342
913
402
327
461
445
904
509
68
8
860
922
284
282
ShellSort - 0 steps
816
778
93
159
153
122
313
282
892
431
864
169
923
501
127
233
347
896
727
328
56
1
415
166
918
344
889
599
248
846
538
903
654
292
339
611
820
205
643
372
874
116
388
704
997
136
150
956
680
910
991
397
475
984
758
598
121
253
914
388
338
630
82
586
583
850
435
786
372
662
724
678
633
851
412
534
450
381
106
641
57
657
63
170
567
68
714
51
876
653
312
965
757
491
841
789
977
979
253
696
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