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.

866
444
66
107
66
995
573
830
320
412
78
261
283
178
120
376
743
637
180
931
699
483
162
356
707
947
598
597
724
327
710
830
150
522
771
742
762
300
608
778
852
430
413
948
497
154
221
451
631
952
776
566
886
717
60
977
63
967
912
955
473
728
865
219
444
662
798
556
526
572
857
350
921
384
388
949
354
111
571
441
206
876
502
719
102
459
289
776
578
643
437
724
730
627
348
883
100
342
296
505
BubbleSort - 0 steps
103
550
737
626
459
84
762
31
280
579
867
291
826
639
244
776
907
243
5
215
596
988
701
814
330
987
661
722
101
683
621
630
829
303
643
668
428
912
975
245
710
440
128
27
476
237
920
703
779
557
750
430
420
34
99
995
603
988
76
512
795
46
288
498
553
307
269
212
379
667
25
918
653
828
786
340
150
19
104
164
161
759
985
97
704
898
706
499
163
202
621
95
633
781
863
495
827
804
393
130
InsertionSort - 0 steps
73
527
961
567
589
644
392
889
18
607
641
320
688
600
83
148
157
698
606
382
712
834
320
561
118
39
501
924
261
697
425
559
491
780
25
837
231
93
985
800
278
618
137
967
568
538
320
507
870
777
605
448
603
966
718
985
458
230
640
893
8
517
400
877
282
839
158
447
247
696
167
906
849
3
447
548
542
706
977
459
434
506
925
749
430
285
129
198
431
275
481
135
61
146
271
672
212
758
810
319
ShellSort - 0 steps
42
422
407
419
511
395
331
979
103
318
122
153
31
303
485
815
955
474
70
19
599
247
785
921
724
539
354
999
793
887
487
61
23
906
926
240
117
454
231
657
619
252
9
969
339
455
636
798
702
116
166
967
170
371
642
910
633
759
670
639
657
438
468
200
894
962
772
702
220
530
216
208
362
987
386
743
841
460
340
810
23
461
612
316
327
480
474
407
496
735
844
763
783
584
984
497
96
929
175
62
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