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.

767
360
780
706
632
175
728
227
269
560
132
861
241
151
601
991
660
149
963
901
327
281
303
103
552
390
13
160
983
442
518
839
98
879
78
238
864
934
819
607
759
167
417
412
78
644
818
967
323
484
1
182
830
603
278
457
494
900
81
721
752
496
813
74
890
458
745
990
471
968
577
231
911
444
525
731
928
702
425
166
27
809
884
479
411
245
930
855
830
647
256
648
881
522
303
815
374
746
864
370
BubbleSort - 0 steps
306
200
617
343
346
798
924
389
122
113
1
684
773
372
465
370
675
642
190
784
15
734
657
715
457
34
926
915
149
186
825
366
311
871
617
849
930
961
605
614
764
544
102
848
555
899
992
831
574
919
725
568
766
42
681
666
432
594
275
964
505
745
587
414
410
191
951
66
961
387
332
110
286
910
373
912
510
282
795
729
140
524
552
743
316
140
935
171
142
561
869
406
770
389
377
73
597
584
827
55
InsertionSort - 0 steps
112
726
562
942
664
738
315
794
410
101
837
983
447
76
7
588
650
225
918
694
850
299
640
343
898
977
88
237
154
562
977
378
640
478
907
332
282
736
225
721
513
943
713
381
291
622
685
580
874
727
898
249
196
812
340
711
864
356
454
644
133
487
510
277
672
563
685
937
579
646
448
358
271
722
635
955
895
217
536
202
543
129
565
377
418
651
83
558
820
30
979
633
505
417
834
724
848
671
333
508
ShellSort - 0 steps
42
937
681
226
605
575
785
35
694
495
888
49
614
164
158
198
262
120
735
719
72
126
476
478
520
689
9
277
16
352
519
499
746
455
625
391
463
347
320
249
979
636
881
48
256
766
185
221
905
84
764
806
605
556
65
758
506
324
105
832
785
272
246
960
620
587
380
980
131
86
39
945
104
266
588
767
682
236
665
151
511
993
855
728
839
356
905
818
637
55
734
907
49
26
398
215
364
778
606
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