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.

142
175
34
630
776
834
593
994
877
146
704
53
266
54
438
706
957
224
15
192
898
370
485
576
689
480
450
827
984
244
808
34
720
132
162
60
398
323
846
644
436
876
194
346
25
958
463
992
447
463
899
362
425
470
634
592
348
237
435
455
473
433
725
450
846
881
361
960
767
60
877
800
124
81
927
13
350
217
608
753
560
629
94
356
521
846
284
125
854
833
649
126
176
121
710
509
920
256
932
19
BubbleSort - 0 steps
880
589
904
340
659
360
667
798
89
114
72
413
408
393
597
550
613
660
313
754
20
149
755
544
281
637
877
737
225
271
857
40
97
63
921
15
255
891
555
432
972
364
318
218
617
485
988
370
723
491
844
86
162
956
519
112
704
282
331
784
214
442
530
703
346
418
324
912
838
786
152
377
612
783
879
93
546
941
162
856
622
90
834
98
697
496
493
64
286
854
580
980
90
396
427
895
440
629
232
446
InsertionSort - 0 steps
992
478
822
716
690
531
998
272
23
688
784
995
802
237
987
74
162
217
611
965
455
986
24
662
959
730
927
384
940
870
304
405
128
488
314
925
669
952
617
629
591
213
79
714
756
546
930
837
958
227
190
869
280
851
385
300
497
211
515
613
397
822
508
894
388
849
757
729
752
469
747
941
645
595
261
31
261
840
262
12
72
811
941
939
102
558
938
490
204
959
846
42
581
634
889
319
962
724
850
200
ShellSort - 0 steps
168
598
545
927
458
604
336
877
612
970
993
216
121
933
187
458
857
387
560
763
330
778
586
199
997
841
962
340
942
929
511
378
136
702
237
421
788
799
405
582
857
814
166
20
674
678
80
527
145
28
587
207
444
978
980
718
929
226
258
137
866
818
115
738
720
140
668
909
712
190
25
675
13
707
468
102
458
55
611
241
491
542
673
420
609
237
908
318
951
490
928
919
892
585
604
883
425
149
58
205
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