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.

78
767
57
773
207
850
145
606
702
165
253
821
199
739
341
844
216
308
880
499
910
823
884
445
536
403
300
874
138
706
109
27
744
226
333
855
139
537
761
892
806
784
46
988
691
876
819
773
49
831
143
134
927
570
547
179
878
969
343
731
415
979
812
213
338
164
440
609
570
976
543
244
35
329
447
821
955
617
253
144
252
89
287
210
769
956
485
142
851
630
555
865
91
100
2
588
151
758
604
299
BubbleSort - 0 steps
195
139
821
329
350
125
870
828
655
270
241
300
173
197
802
277
827
684
280
492
725
367
116
678
106
518
283
644
912
553
48
805
638
218
25
55
580
747
559
107
782
368
433
958
297
876
465
264
14
322
670
809
731
24
209
466
420
223
802
807
546
740
26
109
6
711
815
218
594
975
572
622
844
589
803
204
500
100
153
198
445
83
299
45
961
549
261
87
349
835
793
924
523
118
114
346
548
700
486
587
InsertionSort - 0 steps
601
843
310
691
133
323
218
359
497
775
213
131
146
864
447
159
857
870
386
875
246
309
472
551
403
958
894
938
873
285
636
700
810
401
77
499
131
58
886
359
799
29
333
863
344
656
395
215
551
18
373
61
836
344
637
337
671
546
182
325
413
817
300
49
461
649
924
955
726
978
595
995
23
245
155
204
910
463
532
501
172
172
624
9
873
433
257
634
212
152
787
394
825
410
631
422
304
242
723
969
ShellSort - 0 steps
120
250
751
441
560
575
811
654
615
808
305
338
996
705
741
863
374
828
785
231
102
712
910
8
802
49
541
501
700
903
588
833
658
951
749
453
71
419
964
331
65
811
605
420
119
428
713
758
599
282
569
452
981
846
224
192
590
171
511
748
182
182
945
63
126
608
42
569
424
368
44
743
711
457
839
407
376
110
165
533
848
564
160
824
881
797
700
145
972
927
169
468
420
353
344
312
148
305
456
292
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