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.

860
427
953
196
359
391
421
445
880
384
471
734
502
80
613
440
38
296
662
342
506
957
546
324
899
923
481
459
444
545
403
192
220
21
251
196
318
559
786
519
77
675
674
817
226
700
657
268
329
802
916
660
272
405
841
163
737
860
481
834
881
784
76
339
634
223
710
912
306
334
526
642
675
475
193
640
884
806
131
555
133
981
208
459
47
699
826
944
371
535
774
929
703
10
362
801
698
444
400
192
BubbleSort - 0 steps
1000
667
811
74
992
268
715
28
594
997
30
5
220
354
809
64
179
473
647
649
685
614
229
711
651
626
994
265
561
6
266
442
317
291
723
224
485
860
363
899
168
41
852
902
108
153
78
578
183
635
969
128
300
366
935
70
861
478
475
261
877
672
103
795
28
856
349
488
613
339
763
666
944
843
671
809
989
519
253
895
494
842
45
93
224
488
799
948
253
226
249
836
419
212
636
845
961
114
22
662
InsertionSort - 0 steps
664
570
84
551
453
783
557
839
956
527
164
339
591
199
739
711
938
209
15
700
804
981
373
962
763
947
711
686
296
284
381
310
877
903
973
748
465
568
860
511
916
926
182
491
157
728
352
443
360
963
27
993
701
333
478
474
628
286
820
478
677
379
524
499
182
49
793
93
192
977
576
879
789
41
679
72
316
240
50
673
264
577
809
181
16
171
58
398
757
614
644
302
794
632
27
686
761
910
352
998
ShellSort - 0 steps
258
117
692
544
374
558
857
257
517
197
808
595
170
175
452
843
828
147
200
418
124
217
92
567
579
140
451
802
912
785
569
724
212
405
268
267
132
298
965
847
852
899
374
529
726
516
820
420
692
435
114
498
642
828
123
445
288
916
648
821
649
20
50
312
231
37
827
991
979
10
990
588
801
87
127
818
152
455
706
988
421
770
610
437
628
913
790
266
737
19
95
403
703
530
900
352
344
128
487
247
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