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.

717
847
580
63
67
982
579
979
836
263
517
869
488
359
876
203
572
864
135
420
449
702
517
852
212
826
174
743
905
721
277
120
728
119
565
813
781
589
465
211
308
218
25
723
515
227
973
383
510
477
756
864
247
782
370
719
816
489
370
391
486
192
358
680
628
183
316
991
29
351
408
43
630
830
563
368
766
661
328
462
379
27
769
141
978
773
680
831
151
791
946
948
979
160
881
435
919
210
476
775
BubbleSort - 0 steps
232
750
445
877
434
696
928
597
533
744
154
342
246
132
769
734
110
725
9
429
173
222
419
546
868
591
69
13
473
3
427
637
405
325
726
144
271
776
147
729
792
285
48
137
980
565
905
450
742
689
412
757
69
104
173
923
330
23
276
568
364
345
965
147
573
549
263
244
871
803
884
280
113
876
989
368
786
902
429
809
446
516
6
641
990
542
575
877
762
255
731
429
912
682
484
748
706
35
164
11
InsertionSort - 0 steps
5
646
179
785
842
541
543
796
661
146
290
326
266
528
969
102
61
121
463
884
260
395
577
760
827
380
332
520
706
32
230
905
471
45
196
846
874
193
448
455
632
348
729
589
399
562
59
325
45
791
665
527
726
385
650
917
186
606
377
130
221
710
620
388
437
999
383
506
622
878
942
526
666
994
618
891
515
79
688
535
852
825
607
425
888
576
797
128
752
349
796
864
293
224
946
553
262
730
724
205
ShellSort - 0 steps
918
638
603
308
76
506
328
643
731
367
578
367
48
962
989
59
135
397
310
454
582
219
890
59
716
394
983
872
497
305
442
35
804
153
260
94
907
992
397
962
959
269
58
919
772
243
679
757
606
357
666
117
826
228
257
371
62
416
939
710
506
174
341
305
903
128
902
154
261
768
559
66
493
87
813
59
418
782
991
860
958
624
499
560
90
220
143
662
282
929
268
684
276
934
796
465
396
50
12
486
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