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.

521
825
568
313
79
664
231
579
114
643
149
487
997
753
945
355
471
676
260
393
901
315
991
878
649
79
724
524
156
773
791
139
318
906
841
916
420
240
937
662
724
413
830
921
101
927
293
266
186
46
825
279
623
651
967
752
122
811
335
70
251
259
179
279
505
3
38
190
440
105
352
532
27
806
88
687
898
620
79
263
909
703
443
139
610
911
924
560
701
609
532
687
955
658
56
230
833
386
563
471
BubbleSort - 0 steps
282
622
612
583
182
418
708
790
477
257
463
126
82
723
882
936
129
587
683
818
319
818
870
531
229
459
843
961
983
729
190
450
201
490
297
341
663
194
895
782
611
716
293
308
388
902
754
492
449
709
676
912
877
842
762
241
182
711
448
426
799
725
438
159
911
305
897
626
18
773
797
150
41
259
381
784
192
761
32
643
461
334
669
383
642
275
299
15
918
207
252
183
833
158
570
986
328
325
916
680
InsertionSort - 0 steps
735
367
58
902
526
404
617
187
445
60
197
911
948
697
501
684
108
552
729
320
936
869
32
949
106
621
144
556
626
477
217
203
218
291
408
999
643
630
591
653
570
355
68
126
561
841
58
716
257
212
110
752
615
449
320
491
839
278
344
102
891
552
930
733
567
800
242
73
178
28
489
760
790
529
594
452
294
232
834
779
246
422
441
846
671
788
806
913
790
131
78
28
30
977
464
321
612
772
839
36
ShellSort - 0 steps
794
352
95
766
783
106
578
105
382
629
174
61
570
287
370
448
643
344
647
710
874
370
653
855
418
574
652
941
912
383
582
382
741
473
268
917
48
651
156
340
512
315
11
448
19
244
371
535
68
450
1
816
565
433
731
402
237
749
376
822
984
474
971
153
352
88
9
120
634
578
627
113
921
692
983
110
965
759
911
445
383
292
34
532
208
927
901
739
505
163
371
807
115
142
506
358
649
754
376
813
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