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.

100
395
243
87
485
939
27
706
221
496
300
969
287
334
388
327
123
277
135
484
704
893
595
876
425
271
806
887
992
69
904
788
543
145
328
97
288
735
840
460
441
865
553
229
15
989
384
540
645
622
355
512
724
123
530
692
406
766
578
387
78
438
653
849
148
34
144
747
173
722
650
802
19
816
561
988
165
333
518
943
816
368
406
614
127
951
114
362
774
724
62
362
615
895
987
73
997
515
849
164
BubbleSort - 0 steps
96
952
678
320
558
740
216
319
168
534
893
80
229
824
552
391
868
685
106
108
990
677
625
712
1
970
997
889
245
423
291
162
816
291
734
346
789
782
850
812
119
362
765
889
337
504
303
848
521
857
999
284
796
606
282
325
583
823
155
632
962
494
862
437
270
487
976
336
85
657
89
49
865
103
287
436
824
313
680
278
434
794
647
547
267
826
359
374
941
603
570
379
743
764
572
863
572
842
63
213
InsertionSort - 0 steps
922
465
762
603
879
191
796
780
860
293
212
250
252
391
495
477
712
268
785
479
255
101
184
981
665
981
649
722
224
585
163
170
534
748
641
113
52
28
31
750
179
638
151
104
742
430
430
319
938
238
10
18
258
351
706
62
77
682
516
792
832
729
713
887
227
832
567
147
27
987
366
487
623
474
896
779
230
197
145
711
351
205
940
958
943
980
505
367
861
13
383
133
40
322
533
216
503
984
402
116
ShellSort - 0 steps
59
994
314
423
299
451
865
305
74
823
25
265
1
295
894
324
312
748
730
298
128
54
305
800
192
926
784
398
638
46
568
150
556
348
217
967
810
731
789
728
135
853
210
751
941
491
278
579
843
534
13
794
912
22
253
243
539
557
896
265
934
611
972
891
169
723
749
894
391
859
792
754
624
454
708
141
587
200
499
759
921
659
941
942
558
850
166
997
690
198
362
909
807
649
346
506
560
669
708
773
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