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.

832
493
674
360
662
24
173
639
245
85
398
310
56
980
72
726
694
747
747
983
428
154
647
725
562
350
69
660
257
502
460
497
718
123
232
299
53
178
775
244
933
996
987
807
677
213
343
769
530
769
423
146
878
41
618
273
863
863
646
171
761
558
308
1
470
366
334
630
283
965
890
762
489
193
597
646
73
311
887
129
514
918
497
5
598
202
581
725
527
192
453
136
779
251
950
368
698
296
622
206
BubbleSort - 0 steps
968
641
114
336
906
937
598
44
430
738
483
917
570
419
632
545
333
421
469
91
769
445
81
458
211
498
465
554
546
517
84
845
833
353
118
289
75
958
818
960
405
444
788
20
137
811
352
943
687
900
81
960
252
815
859
592
275
199
373
153
458
890
436
314
350
174
578
72
849
41
888
803
467
148
834
855
604
301
953
606
976
986
639
607
7
795
310
791
553
214
922
266
513
339
399
301
452
569
821
986
InsertionSort - 0 steps
971
416
106
928
603
173
191
715
908
851
387
715
694
714
716
423
132
477
528
453
377
880
75
336
517
827
552
882
766
595
447
702
519
103
921
308
652
973
102
78
781
998
941
101
429
378
301
332
518
860
924
648
87
704
365
545
176
22
634
364
189
145
947
580
610
98
784
268
618
849
498
28
341
211
144
323
997
393
488
896
43
763
237
89
280
861
565
550
964
508
140
839
299
109
9
911
57
772
245
74
ShellSort - 0 steps
947
977
200
880
700
339
464
969
511
688
918
605
937
256
266
83
480
336
348
158
519
942
846
873
340
47
524
511
26
136
247
324
731
603
962
665
248
874
660
106
594
850
994
353
133
684
416
788
968
753
971
487
938
351
219
385
895
83
290
508
732
14
750
154
607
523
3
358
145
264
833
545
86
312
100
678
678
427
639
464
601
895
181
853
934
825
241
512
762
156
360
789
505
828
246
821
766
606
249
506
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