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.

788
840
663
80
355
359
859
186
837
252
141
669
889
753
99
299
973
71
953
981
232
887
198
993
655
974
648
889
955
403
176
339
662
759
8
428
787
298
455
779
284
121
23
173
803
928
913
789
875
924
91
405
200
745
405
528
38
707
361
792
487
91
425
384
615
256
674
920
949
750
13
902
858
354
755
889
638
95
28
481
658
461
86
773
80
496
818
841
499
453
967
852
219
235
198
690
538
796
447
284
BubbleSort - 0 steps
764
477
878
13
772
628
641
465
918
463
20
431
664
43
275
505
232
65
986
276
960
366
981
234
537
567
393
508
367
186
47
843
37
801
862
282
795
547
283
99
979
433
661
503
21
893
919
477
867
367
682
20
326
573
74
223
494
393
774
731
429
121
294
155
950
947
807
370
409
994
532
209
648
645
377
113
107
160
617
392
918
634
922
128
952
220
943
917
646
202
804
606
870
897
296
888
76
105
660
267
InsertionSort - 0 steps
159
504
974
840
312
918
739
388
290
138
725
27
496
25
781
343
801
832
109
118
412
765
896
148
112
603
973
496
270
365
252
759
133
456
311
951
514
199
374
517
292
31
511
795
222
814
638
219
120
297
745
394
467
763
845
141
665
684
51
614
927
383
817
554
305
118
865
804
336
335
681
461
654
157
863
548
344
418
797
838
451
189
878
318
128
766
75
702
323
930
194
566
160
548
787
522
801
113
735
918
ShellSort - 0 steps
300
323
927
317
911
420
809
403
707
201
921
730
960
199
948
472
571
465
396
432
516
703
45
526
38
725
249
809
689
199
183
548
290
285
604
902
377
46
423
774
66
921
311
414
654
486
584
273
230
470
865
489
777
920
250
730
760
477
181
128
247
103
616
802
291
465
215
630
41
91
750
534
65
109
303
68
391
830
676
476
698
689
724
51
85
595
980
753
651
806
916
477
122
304
777
403
662
473
403
769
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