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.

297
305
554
621
726
465
163
99
234
214
569
446
499
156
648
633
589
320
151
183
858
319
810
459
690
184
209
114
322
883
159
962
55
875
866
329
499
596
483
256
521
393
804
775
881
618
522
608
662
301
799
561
401
60
214
905
262
351
263
838
820
138
967
375
79
120
930
122
13
431
665
419
5
507
214
303
91
239
268
14
536
568
958
377
990
970
509
788
462
394
121
249
200
757
989
879
807
842
1000
436
BubbleSort - 0 steps
419
828
299
680
262
868
963
657
374
840
946
689
97
634
80
115
665
95
642
643
560
939
962
996
384
15
289
292
636
992
428
736
605
443
898
912
701
922
131
259
739
252
893
205
995
905
357
44
102
368
66
670
367
697
67
889
701
39
334
706
452
476
629
470
709
661
449
845
542
811
347
766
288
514
137
696
58
346
159
449
374
869
536
351
268
751
911
594
518
984
184
215
753
842
637
688
978
182
931
633
InsertionSort - 0 steps
162
534
877
466
874
38
331
662
625
885
956
555
839
38
342
823
191
642
423
65
761
268
46
873
248
77
139
542
798
766
45
986
241
413
887
196
803
872
956
387
281
727
700
868
342
450
815
899
318
515
572
13
962
630
632
660
171
373
704
585
5
720
832
835
700
177
165
455
18
17
88
123
193
69
152
396
324
659
630
1
170
835
269
912
485
200
554
497
844
331
883
758
137
153
623
809
770
588
425
894
ShellSort - 0 steps
130
557
833
734
891
149
459
785
739
533
324
128
873
144
482
547
942
443
923
305
563
783
985
974
921
682
297
116
285
618
907
286
591
547
104
214
170
110
268
284
138
739
554
686
451
633
71
66
432
418
456
155
869
111
147
263
171
163
435
756
382
414
760
339
686
624
588
574
853
771
100
27
514
291
43
622
487
833
681
296
73
810
111
664
750
687
703
382
926
110
746
830
344
403
151
22
942
636
488
122
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