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.

211
940
451
860
203
255
140
342
359
313
947
25
640
586
865
57
880
330
39
257
27
399
961
839
848
279
827
713
912
917
235
246
85
174
593
982
138
29
32
181
165
284
588
912
237
66
143
50
479
829
747
976
250
868
710
288
112
800
966
635
13
5
820
54
469
884
105
397
744
872
755
493
820
797
321
135
902
376
87
496
69
40
336
113
343
159
44
473
561
423
538
748
406
366
441
743
829
340
352
993
BubbleSort - 0 steps
142
355
925
810
230
197
19
316
926
693
789
539
720
863
208
67
131
881
81
877
258
257
822
600
42
119
68
590
972
493
807
1
940
407
826
845
793
757
713
40
897
259
298
193
20
444
178
790
938
797
337
342
784
509
470
264
569
129
34
884
683
715
4
877
882
551
345
852
383
128
599
968
386
899
604
316
538
513
419
533
585
948
258
446
65
685
374
271
679
108
63
639
924
862
905
414
549
378
937
841
InsertionSort - 0 steps
133
523
533
756
328
674
443
323
552
674
467
632
620
588
575
435
333
842
452
32
16
593
136
305
86
125
268
216
280
411
261
854
131
766
236
967
565
10
989
310
434
248
583
818
97
515
883
133
549
931
839
539
326
441
984
956
499
123
846
607
138
58
778
545
186
867
313
598
936
690
743
690
368
510
896
203
665
124
675
724
529
548
636
802
479
247
935
463
154
890
631
518
691
498
527
125
486
529
323
140
ShellSort - 0 steps
971
205
630
863
317
787
975
40
807
95
342
979
278
978
501
3
392
10
234
894
730
73
561
305
267
884
678
628
973
949
696
628
345
316
807
103
192
56
778
208
519
823
588
821
139
363
14
5
43
460
123
916
179
466
95
145
835
346
665
925
948
187
317
906
312
197
463
715
603
410
399
453
319
910
118
86
820
223
3
871
925
49
652
639
225
167
94
182
325
442
522
921
4
845
555
957
702
814
172
518
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