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.

580
187
27
354
373
201
8
377
379
239
351
731
532
776
414
463
546
763
314
977
156
277
517
263
546
350
514
128
970
859
615
212
793
466
617
36
690
489
873
527
238
602
312
352
74
481
42
799
625
641
63
764
494
248
268
425
949
765
377
425
651
415
265
302
157
78
100
961
507
763
653
640
403
240
390
923
759
649
550
991
168
843
425
940
88
8
49
140
943
177
931
710
276
888
576
871
418
246
457
988
BubbleSort - 0 steps
256
701
351
485
143
61
284
269
985
616
712
952
615
565
409
419
814
338
10
46
226
377
882
743
658
457
862
839
373
194
499
758
845
976
972
311
128
987
446
342
439
639
74
31
478
157
804
551
891
277
224
161
194
210
51
529
298
341
288
592
955
567
676
375
839
461
886
149
951
549
613
393
621
721
999
626
218
713
493
889
69
236
401
644
668
843
741
204
30
444
64
696
210
897
921
586
510
649
198
674
InsertionSort - 0 steps
386
764
888
307
264
79
561
364
75
834
40
731
473
910
381
188
303
357
19
928
376
894
993
169
663
62
638
867
871
161
969
112
138
828
271
295
281
24
106
965
57
21
881
324
107
702
883
393
878
488
876
908
843
769
538
500
579
615
373
708
64
40
770
3
671
51
348
177
570
403
528
380
220
333
403
157
337
424
666
117
51
208
634
584
18
145
729
679
978
451
914
459
110
723
581
778
418
864
617
886
ShellSort - 0 steps
766
755
462
504
872
891
796
152
66
989
728
411
271
936
913
640
70
151
267
887
526
802
604
674
299
73
691
964
779
16
629
412
764
901
387
685
746
891
605
79
995
157
388
752
672
342
590
791
206
311
68
94
133
743
15
207
22
282
953
156
180
615
294
169
144
353
523
140
79
551
591
75
971
2
64
615
40
969
410
873
927
711
495
994
446
339
350
569
80
159
52
831
582
628
314
73
617
352
810
553
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