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.

951
239
325
383
814
959
648
632
96
506
929
522
696
241
544
988
489
861
900
444
407
172
663
822
621
65
163
268
140
207
753
650
807
567
99
202
867
661
176
819
838
260
997
154
606
813
195
703
951
310
944
906
191
144
777
62
505
528
635
627
260
149
412
53
6
931
371
757
526
162
984
223
399
410
321
566
557
780
809
462
700
272
228
616
655
227
978
871
244
566
967
317
505
583
405
90
677
989
197
169
BubbleSort - 0 steps
442
220
848
571
190
680
962
194
200
629
883
659
541
947
527
314
257
940
80
790
516
910
692
667
581
702
456
188
818
817
699
327
334
357
863
870
22
332
254
827
729
977
917
69
704
424
475
173
643
753
65
773
835
400
343
166
850
227
925
473
828
403
770
377
942
477
15
3
387
221
552
825
575
294
660
874
939
982
524
739
432
892
947
157
293
184
752
342
660
416
28
656
677
814
773
921
501
403
66
792
InsertionSort - 0 steps
241
142
659
90
139
474
925
188
654
72
309
110
882
630
621
909
20
515
542
3
832
297
298
103
107
229
465
961
388
900
524
376
781
180
760
26
382
723
323
937
884
799
272
896
93
149
490
691
621
731
832
264
354
726
236
337
603
734
726
964
660
749
77
375
571
828
128
209
329
473
832
516
727
693
609
841
494
386
915
978
218
776
223
641
263
818
875
752
708
962
530
146
547
112
855
712
244
498
716
790
ShellSort - 0 steps
60
168
85
785
926
416
757
396
81
542
757
905
129
270
877
910
297
685
604
255
313
143
777
185
255
794
52
626
853
837
194
319
694
589
760
468
683
8
571
161
476
17
458
934
473
169
773
987
637
251
667
363
965
936
186
721
86
382
929
196
644
786
182
474
969
556
176
654
425
44
282
960
838
49
150
447
863
609
173
150
183
173
70
740
760
926
486
730
95
301
931
413
751
367
648
27
88
589
414
951
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