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.

883
109
779
897
937
990
333
187
106
486
871
935
175
319
902
635
775
234
769
59
167
888
442
870
966
460
843
668
359
822
316
788
257
529
76
378
991
105
175
670
201
482
656
247
269
737
371
827
692
535
98
499
433
912
706
564
247
359
732
802
849
265
887
970
919
481
180
319
77
841
190
912
618
14
197
593
40
796
122
128
534
635
221
188
606
678
345
392
262
418
774
984
593
574
421
694
800
280
392
777
BubbleSort - 0 steps
202
181
333
351
893
671
139
126
63
238
507
458
857
662
647
614
508
482
931
86
855
577
586
618
262
565
507
993
536
511
435
178
925
807
206
424
811
130
307
599
293
340
163
143
147
40
462
959
90
902
805
256
205
936
59
71
35
836
430
254
775
675
713
217
466
337
624
798
734
903
981
359
178
892
804
361
1
721
21
376
85
169
870
394
978
8
296
947
20
132
429
894
155
186
524
447
673
788
760
557
InsertionSort - 0 steps
636
280
987
654
971
799
420
301
675
44
741
690
720
391
348
268
926
40
367
447
935
426
243
129
511
65
543
173
73
175
840
29
75
130
784
769
342
788
136
210
981
24
165
704
624
119
741
707
267
728
358
908
839
172
834
740
367
742
698
188
377
342
939
195
254
10
887
25
820
121
537
262
365
375
814
454
447
397
735
460
574
681
427
34
65
52
502
489
275
430
262
657
244
646
89
278
494
370
729
532
ShellSort - 0 steps
621
450
854
873
866
215
493
822
472
163
1000
124
830
413
961
342
824
20
305
880
676
805
3
962
60
743
765
131
268
910
104
405
259
437
823
297
75
948
312
443
742
749
750
451
193
639
373
551
429
766
774
782
896
706
805
269
618
925
490
509
672
425
811
178
177
367
57
274
92
497
123
204
363
629
538
354
124
788
862
791
845
49
372
379
58
393
523
967
642
67
797
500
191
216
149
385
286
100
777
937
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