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.

697
503
279
283
872
642
893
699
377
89
53
983
949
985
901
238
541
273
582
835
260
280
129
729
783
195
654
8
14
949
899
760
703
599
300
45
928
391
601
999
667
891
270
491
275
877
816
352
684
808
126
569
262
804
956
565
758
824
578
974
542
392
480
982
334
761
529
809
724
782
71
310
417
256
812
123
56
637
502
160
142
299
974
592
74
959
775
344
315
786
902
651
552
269
506
608
137
409
943
358
BubbleSort - 0 steps
610
589
437
548
871
237
798
488
969
914
421
737
988
342
514
903
947
526
111
882
386
56
259
968
26
514
87
651
943
980
907
184
57
742
159
331
951
915
978
540
368
434
822
261
381
498
93
920
451
504
674
679
288
332
438
830
987
368
356
191
695
575
226
168
394
423
6
459
72
95
630
821
55
657
820
132
477
675
306
541
811
927
670
510
407
330
792
591
594
76
966
838
11
37
736
15
708
831
764
40
InsertionSort - 0 steps
338
369
353
61
151
165
979
66
680
445
461
291
653
590
867
192
993
359
430
493
730
725
235
483
726
911
581
335
718
192
476
259
244
168
324
532
808
908
814
474
330
783
426
744
829
445
530
378
897
8
609
596
786
246
811
939
783
59
11
954
502
797
609
895
711
631
450
848
16
710
752
140
71
459
66
97
827
363
949
701
562
840
477
396
221
938
995
289
355
725
693
514
980
472
343
270
253
854
941
289
ShellSort - 0 steps
578
106
392
373
991
571
213
913
635
932
472
417
47
675
292
310
344
768
467
688
347
180
999
620
25
71
323
710
67
942
771
738
333
708
500
917
341
335
592
591
893
638
404
561
502
939
202
974
942
436
422
524
517
626
526
159
897
793
33
844
511
906
603
631
957
338
942
463
674
393
514
985
39
710
709
8
899
328
706
987
809
627
307
626
583
122
310
603
391
889
848
39
993
400
657
877
514
411
517
549
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