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.

874
934
695
526
881
83
887
945
217
390
701
147
721
461
513
107
887
197
764
426
180
78
708
36
93
805
783
968
479
729
523
808
377
910
101
860
304
957
579
787
719
925
230
842
485
439
837
214
928
159
671
38
83
291
29
424
881
639
65
164
366
756
688
877
707
969
958
894
614
413
223
927
318
732
226
187
945
735
315
63
132
899
823
931
319
881
411
495
308
323
471
160
11
450
728
955
385
718
654
898
BubbleSort - 0 steps
141
893
303
22
392
515
911
812
995
55
700
469
792
937
809
826
479
107
809
710
917
433
535
92
175
233
962
534
805
844
505
301
505
536
144
983
689
755
688
621
411
697
422
650
585
57
881
76
672
279
545
174
694
101
501
681
311
877
856
665
356
181
761
272
488
782
455
862
356
62
525
74
561
3
595
927
424
633
868
342
50
49
168
874
447
967
122
78
20
375
92
689
199
359
338
938
180
625
308
491
InsertionSort - 0 steps
510
963
529
35
156
694
272
112
918
883
258
94
439
936
970
219
657
366
237
874
491
920
599
115
448
278
955
154
565
773
365
89
487
959
38
268
889
264
430
265
909
447
363
522
413
680
854
493
374
829
617
587
669
762
799
901
839
55
795
448
861
620
832
731
797
609
418
842
273
411
600
709
753
745
331
678
19
382
493
539
75
763
833
447
311
61
613
857
364
473
144
808
859
30
734
59
174
198
288
645
ShellSort - 0 steps
196
939
855
708
579
58
681
316
259
147
338
950
247
800
980
343
919
215
726
930
673
460
271
534
464
393
384
397
76
86
130
322
997
753
671
411
346
201
918
705
987
859
260
360
193
33
515
532
940
397
336
260
243
443
969
799
844
515
626
667
183
702
310
933
911
54
790
312
91
357
852
886
959
348
783
746
836
9
312
714
661
856
884
897
943
347
457
375
61
649
890
220
712
907
780
537
932
603
756
382
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