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.

191
14
83
781
69
829
672
452
263
177
114
28
134
558
544
839
982
939
909
52
377
797
639
307
1
400
369
431
729
140
210
554
431
751
147
763
850
834
588
438
639
126
344
141
500
706
830
645
814
464
179
676
90
143
134
44
130
707
239
961
636
284
511
564
897
570
730
88
579
553
540
325
953
6
394
525
466
447
935
604
324
644
353
771
612
839
696
9
838
483
387
224
213
352
412
447
424
319
380
70
BubbleSort - 0 steps
327
808
316
127
317
999
893
345
852
705
535
629
203
411
780
745
351
805
775
874
438
639
801
697
675
427
646
753
102
94
268
211
160
774
769
912
293
823
258
146
749
373
338
183
917
399
542
611
58
925
753
535
67
480
819
125
488
188
689
73
433
927
216
859
92
935
217
169
23
564
142
98
561
676
726
455
665
496
893
418
511
286
262
920
690
390
766
522
822
984
754
212
508
528
941
541
387
94
848
404
InsertionSort - 0 steps
783
560
941
496
442
520
292
754
644
838
530
387
12
715
985
472
595
122
386
470
659
287
459
122
479
857
985
532
523
475
880
676
398
622
195
805
313
385
181
399
835
327
911
934
794
755
499
749
807
50
398
996
821
37
776
794
99
152
432
318
569
371
130
450
57
47
166
905
170
374
268
668
911
589
146
368
909
697
181
693
815
405
508
460
565
414
728
135
776
958
444
329
649
352
769
150
173
110
251
508
ShellSort - 0 steps
570
987
153
946
781
37
594
896
332
802
721
894
446
489
842
473
93
149
220
17
543
411
148
949
198
750
528
496
293
534
715
139
682
613
474
24
919
168
481
912
1
510
874
912
155
689
334
104
227
34
342
729
202
452
686
237
518
956
643
226
971
401
90
394
497
459
539
479
8
988
542
368
565
960
289
752
946
386
900
378
711
128
10
68
595
818
923
433
741
286
921
63
313
742
50
244
30
930
205
390
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