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.

142
98
674
167
675
328
748
226
608
128
555
390
729
457
415
716
934
255
870
368
835
853
652
517
168
504
779
715
615
355
185
927
333
401
74
680
726
184
698
30
368
549
601
376
297
751
73
666
900
933
233
594
746
85
566
438
776
391
278
146
628
902
217
846
651
323
557
493
69
451
791
309
75
244
262
709
656
426
407
753
629
525
604
75
204
195
697
824
862
663
37
430
133
968
521
53
862
696
808
384
BubbleSort - 0 steps
609
225
158
816
790
899
814
249
716
584
194
307
204
511
554
32
377
635
860
619
376
686
544
233
548
783
793
49
412
298
32
594
354
859
705
279
233
435
581
280
159
203
399
747
497
774
387
454
942
344
332
164
437
248
898
27
675
944
937
419
657
984
338
273
418
851
466
856
128
741
375
96
501
160
973
724
884
290
803
876
362
323
110
370
568
950
130
785
681
258
741
454
181
798
834
184
276
649
688
57
InsertionSort - 0 steps
673
796
108
785
292
31
715
724
991
472
446
196
581
38
305
746
752
947
12
684
349
248
300
331
654
138
903
137
89
824
659
995
792
484
936
260
499
76
726
538
261
70
126
1
712
312
194
83
619
543
923
86
53
830
474
489
592
653
16
652
686
616
836
388
982
274
606
543
548
500
104
313
234
961
467
222
927
850
732
39
110
658
441
735
101
388
733
121
131
413
797
138
292
793
212
443
572
180
1000
113
ShellSort - 0 steps
814
130
883
517
95
182
638
936
867
924
98
226
450
269
192
139
858
525
699
353
395
1000
878
1000
783
698
960
428
966
844
183
118
851
733
326
352
251
762
722
122
456
615
569
427
680
842
801
141
313
319
314
424
76
567
822
911
924
554
942
921
132
978
622
938
527
647
638
248
374
267
707
791
22
294
944
741
177
861
543
410
39
974
107
547
816
70
8
9
598
750
299
778
893
112
778
76
653
678
525
815
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