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.

174
822
213
67
617
839
252
232
178
16
327
835
173
763
346
550
989
193
653
632
379
981
392
654
331
557
881
998
504
148
114
10
501
868
536
249
967
447
525
661
757
30
358
345
44
17
641
684
714
947
39
336
601
137
695
875
296
389
419
17
603
749
388
885
546
536
184
104
880
970
814
699
63
679
333
454
432
700
446
795
626
840
192
222
352
730
949
172
336
562
12
611
769
86
929
270
296
461
721
151
BubbleSort - 0 steps
715
456
842
701
118
405
872
60
869
860
977
812
137
166
595
307
64
717
310
289
765
633
61
683
792
238
560
480
172
224
858
986
826
687
440
989
115
923
534
934
262
849
81
593
347
869
324
906
441
454
271
17
146
974
528
139
259
152
994
448
289
87
860
607
686
931
867
83
461
113
471
854
543
63
91
825
762
84
413
338
618
852
959
941
573
103
372
351
468
224
603
735
598
862
149
852
725
40
9
225
InsertionSort - 0 steps
215
175
353
865
790
845
301
373
438
789
89
222
673
451
759
263
163
823
683
754
104
885
145
480
290
221
807
126
416
815
840
772
954
903
736
374
997
111
169
78
539
953
280
306
258
478
400
986
836
591
889
570
198
462
145
632
250
404
220
423
709
756
815
15
756
786
422
838
823
722
881
3
967
427
764
632
360
740
929
386
588
259
812
903
583
191
664
675
838
570
164
810
523
349
202
86
587
25
1000
860
ShellSort - 0 steps
689
86
106
942
124
426
178
75
138
965
820
488
904
897
620
872
141
301
705
131
566
177
905
53
894
900
390
904
839
133
590
622
61
531
877
957
280
116
717
421
487
201
274
600
74
585
501
188
284
419
608
480
294
362
591
410
60
331
347
499
103
651
715
167
591
183
986
473
877
649
868
595
564
413
994
136
698
353
433
116
763
941
364
427
74
29
234
865
514
538
864
102
722
804
697
490
602
592
987
837
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