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.

488
893
567
126
542
542
657
320
256
252
357
384
722
355
85
145
855
764
76
671
661
327
945
418
563
614
337
53
680
519
840
755
975
488
695
896
228
386
97
101
278
253
505
311
794
527
3
71
543
643
361
568
492
266
143
869
917
328
454
200
882
727
617
507
620
647
112
398
174
227
753
355
364
11
211
616
856
980
619
682
724
940
814
729
577
869
99
10
766
551
47
892
69
890
570
826
224
91
94
383
BubbleSort - 0 steps
337
531
336
127
462
273
181
753
470
594
134
451
207
815
616
546
718
885
80
323
436
72
267
132
574
204
833
728
616
963
124
604
506
8
425
726
97
250
172
696
491
496
978
974
45
755
903
771
164
561
516
947
355
459
805
576
337
463
790
5
956
576
705
703
794
106
982
211
643
246
956
6
145
227
637
971
7
469
619
886
501
972
735
516
747
364
272
699
286
807
259
61
623
512
444
583
270
982
920
776
InsertionSort - 0 steps
545
908
561
506
297
83
503
5
22
135
283
323
73
916
204
206
849
673
553
738
966
57
859
789
172
919
966
97
509
793
5
347
469
299
254
803
844
722
196
625
161
672
434
215
276
309
50
776
853
981
356
336
630
159
266
292
818
65
369
505
398
912
466
59
174
371
60
309
342
924
485
351
230
432
514
606
571
979
415
586
954
232
667
604
520
450
596
969
345
761
65
161
411
395
951
63
591
494
956
795
ShellSort - 0 steps
870
840
201
148
534
921
24
324
842
323
900
234
433
958
535
311
986
682
815
989
67
365
419
852
148
745
946
381
208
697
588
159
172
152
593
266
346
405
718
500
807
260
864
866
558
857
174
380
699
845
998
221
154
892
737
751
802
661
396
729
156
66
542
940
458
818
322
387
427
975
725
630
385
979
870
778
768
695
786
371
663
591
865
424
251
856
554
659
358
16
767
170
539
752
970
655
348
538
742
497
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