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.

817
691
516
57
332
797
495
591
419
265
628
895
472
400
473
227
545
650
912
375
517
738
686
868
632
364
786
557
928
397
267
143
260
163
1
861
985
701
956
343
75
577
597
900
607
116
335
164
45
592
596
791
857
105
857
829
241
351
502
75
41
644
614
832
247
628
198
989
695
623
228
968
335
480
159
595
456
938
823
518
579
780
98
35
961
371
340
376
293
384
685
102
228
24
739
339
948
442
576
427
BubbleSort - 0 steps
587
75
750
602
25
955
261
752
720
506
402
887
601
242
631
37
194
936
497
562
46
674
727
603
722
680
835
88
664
171
709
979
920
89
402
258
564
340
54
987
846
252
851
942
21
217
548
120
929
778
946
730
541
920
745
874
30
892
370
990
220
709
390
778
761
624
916
901
914
521
69
303
982
212
334
164
404
898
924
102
156
433
212
64
61
915
717
995
498
499
669
677
464
393
564
663
707
981
425
47
InsertionSort - 0 steps
294
99
554
15
918
190
189
283
731
139
712
118
954
39
556
664
180
371
239
501
165
604
584
64
772
463
959
492
190
282
195
117
972
729
863
639
889
192
731
251
147
380
46
425
409
183
482
546
893
754
187
226
246
559
956
900
832
483
424
826
673
139
597
423
490
983
33
498
439
982
368
443
806
413
790
562
37
662
757
921
821
81
363
130
26
663
926
862
151
674
120
914
439
818
848
660
410
159
512
390
ShellSort - 0 steps
573
800
256
685
220
639
309
712
923
897
276
271
927
372
634
357
115
260
188
771
120
892
524
108
829
267
174
296
884
930
212
833
247
588
871
348
19
980
40
479
204
346
102
947
10
316
371
107
118
840
669
672
647
9
951
253
548
668
595
106
863
188
193
590
614
990
894
461
539
211
640
647
310
489
360
645
657
617
528
25
232
332
1000
120
573
104
986
760
299
990
953
557
106
916
626
315
767
943
143
386
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