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.

965
447
883
287
625
988
586
968
373
923
382
607
103
283
993
229
69
708
43
55
518
23
242
416
696
700
494
827
147
271
798
603
696
496
551
969
64
904
745
763
532
421
966
226
891
431
224
804
96
535
90
455
9
980
319
17
744
191
215
240
241
817
579
400
963
908
987
876
114
874
493
912
887
890
203
541
315
214
256
717
652
386
787
812
923
83
799
196
228
935
199
357
449
465
319
551
846
657
650
412
BubbleSort - 0 steps
599
339
50
826
861
794
545
865
315
688
599
203
68
866
436
899
557
73
283
327
200
390
760
487
939
950
665
996
969
133
940
771
569
379
944
894
773
359
759
554
817
698
378
559
783
27
239
295
816
575
495
604
113
917
311
605
521
370
485
281
977
489
897
319
891
95
661
585
459
195
709
195
122
432
326
808
378
298
151
610
277
697
960
711
633
270
177
931
276
752
742
246
595
581
252
154
455
409
822
262
InsertionSort - 0 steps
698
299
597
938
39
190
123
857
326
500
399
523
405
556
346
150
579
747
727
119
279
845
973
767
247
339
607
668
574
625
421
36
472
399
342
281
145
6
495
308
6
529
365
142
556
82
303
300
914
278
270
782
783
280
185
71
490
123
709
482
59
649
999
710
317
210
573
220
481
792
562
420
727
3
816
358
913
794
5
847
560
945
201
945
601
847
291
218
792
981
828
454
167
993
451
504
124
77
228
371
ShellSort - 0 steps
512
824
636
107
14
127
703
786
149
576
665
563
445
905
781
838
516
831
368
889
260
857
313
154
527
433
353
269
926
471
769
445
766
445
608
18
274
415
179
874
116
792
666
194
233
627
510
302
160
822
135
359
211
666
413
413
833
172
975
250
36
24
451
383
837
599
829
568
99
245
362
788
510
416
853
411
614
716
414
296
526
736
25
178
253
622
112
896
113
906
744
501
59
969
578
70
598
17
245
613
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