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.

918
831
476
526
48
938
393
628
739
53
236
219
230
259
876
177
823
982
164
423
344
332
734
928
974
58
722
90
86
437
881
883
353
587
773
858
708
890
709
728
675
224
186
975
390
797
140
258
657
838
927
616
999
774
41
153
268
443
490
610
621
202
13
874
131
638
238
855
268
777
245
894
128
787
869
445
321
135
632
778
941
833
248
299
271
85
977
793
748
256
471
846
667
742
37
703
777
63
841
330
BubbleSort - 0 steps
433
815
109
755
144
733
449
582
717
452
215
422
356
332
311
344
311
393
403
991
586
711
285
3
451
398
692
462
218
559
939
123
462
861
670
826
606
427
166
214
692
183
226
794
1000
115
875
245
921
349
65
934
323
647
27
592
965
447
232
296
790
915
892
551
606
267
785
327
233
820
514
151
242
747
291
691
562
260
639
878
869
830
988
747
42
90
136
427
816
835
87
599
558
139
739
327
566
372
451
837
InsertionSort - 0 steps
589
345
200
677
184
121
395
540
88
336
768
264
52
34
989
759
54
602
428
304
621
416
665
652
622
725
725
188
708
184
857
388
76
160
930
678
588
477
615
926
566
875
115
568
1
748
764
791
624
1
236
934
650
128
899
272
215
924
745
977
971
475
281
859
435
149
634
25
552
181
826
274
739
79
691
610
948
345
737
885
673
89
942
797
743
511
401
834
631
841
2
454
480
934
120
862
130
193
333
425
ShellSort - 0 steps
494
467
732
674
304
556
975
71
955
301
696
353
278
352
580
693
444
778
961
677
489
297
482
973
918
649
622
499
828
328
311
783
346
659
863
364
900
7
630
809
181
942
494
821
865
42
49
174
777
347
450
75
331
328
674
154
264
101
483
46
689
29
764
190
417
712
214
2
807
439
764
235
219
485
561
322
634
104
697
719
115
11
993
158
4
512
665
827
576
281
903
347
868
768
813
312
627
435
62
486
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