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.

96
721
535
498
653
860
578
923
901
461
309
174
2
59
307
759
822
904
441
543
916
265
930
276
261
237
514
923
361
110
786
458
647
891
910
295
933
171
827
622
141
254
796
24
791
127
663
18
199
776
77
156
291
863
749
212
160
126
118
605
189
721
630
737
58
199
30
46
398
446
281
488
86
925
808
925
732
351
950
674
820
762
223
16
993
934
361
305
522
235
405
994
542
341
411
339
397
594
345
713
BubbleSort - 0 steps
663
603
649
491
421
550
996
126
605
521
183
59
608
557
478
669
472
716
754
6
77
787
130
987
485
936
22
453
64
784
757
221
383
884
829
128
426
395
19
387
245
305
906
595
413
552
901
635
427
864
287
499
757
115
428
137
620
396
892
202
432
581
379
159
72
523
772
884
532
401
593
927
145
903
432
834
397
451
761
845
871
555
759
331
499
616
753
773
849
426
800
722
180
450
144
85
513
64
343
608
InsertionSort - 0 steps
648
974
663
588
434
149
122
977
300
393
845
956
495
911
851
173
829
514
447
120
734
410
858
553
258
62
425
883
447
938
319
481
821
22
810
456
199
198
549
356
653
551
121
335
516
851
149
412
614
490
341
254
314
388
835
206
779
627
130
329
563
530
61
182
172
13
32
564
985
479
453
824
740
283
547
846
686
957
874
571
686
756
895
442
890
953
500
468
288
577
260
453
195
58
380
954
549
665
900
849
ShellSort - 0 steps
357
604
795
994
941
542
894
304
558
533
251
721
313
680
624
499
47
759
7
505
958
214
125
372
368
248
668
328
101
229
185
595
373
111
31
712
486
701
792
648
47
283
623
798
986
25
784
115
882
368
930
631
789
687
113
652
398
956
847
529
20
723
865
725
512
437
589
296
728
168
141
664
387
988
916
274
766
56
272
163
460
754
79
227
758
311
992
663
857
529
503
161
487
812
496
876
600
4
319
626
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