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.

221
646
374
894
974
305
489
140
514
864
952
787
899
349
752
604
257
695
646
833
947
112
137
292
228
792
732
555
41
219
474
10
228
844
489
112
486
843
708
6
795
218
733
879
61
661
850
158
737
874
609
335
878
492
781
716
11
564
37
671
567
358
588
938
280
133
241
30
132
561
116
858
516
914
956
274
227
653
296
960
747
573
628
771
462
451
853
747
197
400
111
948
896
955
955
547
760
815
831
772
BubbleSort - 0 steps
845
989
480
590
276
593
860
484
438
207
535
698
52
852
195
476
270
158
775
22
497
644
391
744
271
659
384
727
220
395
226
316
598
905
938
954
538
949
73
776
883
806
10
609
58
288
476
247
88
207
721
297
341
727
287
510
673
291
117
675
953
159
429
404
54
57
784
20
405
134
772
833
233
65
828
749
819
271
237
815
835
717
799
462
439
535
841
660
371
193
179
915
371
74
943
836
269
327
897
241
InsertionSort - 0 steps
30
235
142
503
461
397
600
217
569
279
174
248
618
669
809
975
127
568
229
363
88
789
823
667
766
549
550
750
181
457
709
134
562
697
526
211
536
190
282
351
327
224
417
211
401
899
913
36
741
130
176
271
6
460
642
399
494
43
692
903
607
225
443
138
704
212
711
151
378
238
546
253
159
65
129
904
900
569
321
606
461
782
152
255
325
442
991
665
935
58
213
229
518
17
264
856
732
361
733
674
ShellSort - 0 steps
343
528
579
381
565
899
354
388
395
933
640
18
295
633
22
568
866
444
532
179
534
952
286
207
532
473
593
777
644
874
378
748
992
160
946
157
870
884
367
323
154
222
500
987
179
865
838
926
559
476
914
561
418
343
12
35
811
283
554
624
154
633
728
774
497
593
282
150
406
352
672
535
652
616
891
589
81
700
358
393
124
595
520
64
188
850
452
529
797
677
302
193
554
238
879
10
499
326
798
956
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