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.

752
535
802
977
659
148
94
554
329
317
620
470
103
553
120
84
300
68
562
104
884
6
876
204
971
794
724
810
910
142
579
643
106
29
147
118
135
345
238
467
553
795
83
743
905
332
755
396
168
328
563
652
3
224
789
833
180
164
417
912
222
901
420
297
947
751
537
863
263
467
852
71
892
829
359
107
390
485
668
356
878
766
628
856
735
59
474
597
934
941
848
464
777
663
66
234
906
828
455
493
BubbleSort - 0 steps
643
697
735
165
43
331
754
707
14
463
643
864
293
437
49
893
620
746
452
197
759
969
643
926
217
324
394
435
833
377
759
829
755
348
181
927
816
172
508
447
843
545
399
132
380
99
357
62
426
66
30
193
103
191
69
111
563
913
90
376
538
559
22
118
117
148
826
580
540
990
15
313
33
108
32
780
760
942
523
560
824
735
288
447
992
541
482
69
4
76
880
532
192
743
617
94
502
888
568
622
InsertionSort - 0 steps
643
757
347
361
145
245
820
809
180
247
427
234
932
274
804
15
68
789
991
710
981
900
866
799
370
56
937
372
866
722
90
151
256
257
789
934
820
584
391
729
876
809
359
781
673
578
175
293
572
156
949
891
395
152
199
528
849
255
986
800
54
538
566
873
936
174
807
532
543
18
904
549
729
580
427
563
139
169
411
716
946
602
344
126
328
588
938
982
528
539
382
433
409
681
818
557
47
920
371
662
ShellSort - 0 steps
107
529
296
953
699
314
639
296
304
359
739
190
139
351
81
146
810
96
144
25
651
39
790
200
498
705
983
481
851
145
823
301
813
682
123
978
316
401
707
302
389
554
734
414
9
573
178
592
715
118
403
653
802
249
824
601
380
990
821
670
134
896
947
731
715
981
282
800
911
866
563
255
718
608
548
958
510
394
100
403
177
297
119
114
842
533
623
837
382
54
250
970
134
771
789
917
524
149
482
672
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