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.

232
61
363
649
726
915
693
296
20
245
908
826
520
636
345
417
966
263
788
392
391
984
163
478
7
424
898
114
744
503
102
890
587
473
907
69
300
51
285
17
823
200
499
535
827
150
925
897
833
858
146
692
126
386
913
145
770
700
866
380
515
758
570
473
261
352
19
258
536
643
425
976
493
55
27
664
979
57
807
506
784
679
423
278
313
462
152
898
953
569
803
999
530
633
420
926
585
213
223
94
BubbleSort - 0 steps
666
769
733
406
391
803
819
139
749
172
331
139
804
643
951
269
413
426
810
927
130
933
812
318
78
471
328
914
145
250
572
833
482
693
686
999
270
599
996
9
513
836
946
827
369
72
618
942
73
182
797
55
826
954
541
379
19
874
754
760
197
69
665
386
823
274
595
442
499
477
292
615
220
388
205
347
393
624
185
345
871
186
884
926
696
810
706
236
614
497
14
281
882
132
724
718
451
102
785
646
InsertionSort - 0 steps
100
184
663
975
439
299
669
868
124
81
604
414
552
89
324
803
381
794
806
407
20
503
817
570
391
402
722
722
329
775
920
725
574
822
31
613
21
410
485
570
536
774
291
782
368
828
833
912
168
479
497
591
940
643
723
86
216
166
541
821
666
427
501
70
812
460
659
356
281
32
477
106
308
826
438
686
391
752
23
79
757
154
494
164
899
211
206
128
898
897
78
100
535
17
675
775
594
221
590
210
ShellSort - 0 steps
505
358
550
39
34
790
339
813
157
249
173
249
186
710
207
568
254
307
839
30
413
35
172
720
314
438
395
139
902
505
23
8
319
457
580
32
636
438
731
900
803
30
925
38
517
155
292
272
30
94
857
127
926
917
72
544
393
840
921
514
822
272
129
117
781
284
747
467
519
811
652
223
849
219
4
126
811
668
511
146
97
737
351
607
135
460
103
939
245
488
949
342
574
790
196
62
347
690
166
413
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