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.

286
117
22
800
64
237
426
623
623
996
4
134
428
555
71
72
70
781
49
919
669
981
541
563
894
559
810
203
441
633
386
659
498
240
523
227
667
208
757
557
433
969
309
21
35
578
195
289
605
103
650
472
497
1000
721
603
641
11
166
449
919
695
571
362
677
668
722
936
333
110
864
79
238
311
291
264
490
155
288
230
562
694
184
963
368
78
810
319
917
739
532
424
267
621
959
857
851
106
159
232
BubbleSort - 0 steps
139
84
373
911
863
875
379
389
357
822
44
793
804
727
253
861
274
57
305
208
54
90
183
580
389
921
336
261
834
564
496
806
201
323
903
874
145
304
74
865
771
630
209
305
413
749
223
217
78
901
860
120
837
686
908
499
967
961
392
981
464
317
491
215
87
58
511
708
975
343
158
569
203
934
612
828
578
780
69
445
823
546
767
758
641
818
396
450
588
30
979
201
587
465
222
376
409
129
239
971
InsertionSort - 0 steps
618
239
442
156
343
233
195
707
997
547
407
189
354
480
383
283
806
12
481
394
87
12
535
583
68
361
923
447
218
840
164
789
648
78
920
543
909
841
147
284
336
682
547
639
456
326
2
966
398
499
121
611
260
110
913
95
431
826
804
364
616
512
220
188
786
596
494
398
35
73
182
554
891
622
424
431
5
323
65
369
734
212
272
501
784
173
818
342
358
682
261
864
465
989
550
757
783
124
989
632
ShellSort - 0 steps
11
785
329
226
575
404
844
617
536
973
928
748
221
785
10
347
188
955
838
428
530
748
51
418
422
801
69
994
676
500
125
424
468
731
94
283
356
584
849
441
957
322
456
101
795
907
26
540
342
582
528
803
689
316
456
997
662
914
472
775
343
209
563
379
571
742
714
961
1000
336
799
148
777
164
934
757
803
495
970
61
59
937
461
499
890
995
904
832
489
967
396
172
833
885
496
837
180
993
942
991
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