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.

165
992
83
607
296
311
188
925
641
894
740
606
258
380
954
894
119
789
46
866
142
732
100
994
675
368
995
431
335
845
917
451
134
352
26
127
423
167
48
371
866
182
652
381
709
242
645
42
750
864
744
81
422
674
219
495
959
800
192
587
130
886
26
825
94
854
756
983
540
622
781
98
297
898
195
292
474
889
888
26
847
191
765
415
737
230
471
423
904
418
263
217
380
991
291
562
494
379
787
489
BubbleSort - 0 steps
121
964
665
946
43
535
607
658
718
916
865
550
65
993
731
761
551
998
859
658
811
750
189
811
885
25
930
216
607
167
826
12
607
493
444
725
937
833
380
276
27
61
812
275
303
560
945
980
188
797
650
877
512
854
35
425
595
416
849
805
622
299
959
660
44
293
421
853
625
817
750
534
559
593
285
872
970
367
447
144
899
678
838
984
466
396
189
274
889
107
872
153
963
823
620
711
793
701
965
737
InsertionSort - 0 steps
238
548
313
398
29
305
450
471
708
626
142
403
197
857
557
750
406
672
849
75
826
799
656
395
731
880
207
518
409
703
389
51
157
633
996
129
466
481
393
75
607
301
778
315
676
904
183
447
950
654
317
721
929
889
957
148
882
338
969
95
172
208
553
89
637
306
736
454
494
735
442
499
94
43
492
223
773
314
220
579
399
857
782
160
514
823
678
823
135
729
83
674
511
351
22
284
139
619
247
22
ShellSort - 0 steps
42
567
110
474
295
668
234
158
204
778
769
627
237
338
271
826
643
474
28
627
330
335
723
217
639
746
219
709
288
523
362
627
302
696
200
504
870
833
790
831
856
974
799
240
721
998
45
215
380
253
806
738
90
375
460
904
197
643
585
262
269
751
420
745
527
950
553
110
634
978
505
631
676
989
722
288
323
349
950
756
923
816
192
114
634
784
230
724
557
757
686
841
554
902
637
95
556
862
256
524
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