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.

882
3
859
116
314
4
268
749
780
146
412
331
250
747
484
636
692
776
203
940
865
977
751
17
836
853
686
247
197
942
95
587
493
453
13
160
125
601
188
976
612
515
183
325
926
156
341
524
805
902
729
904
326
270
279
990
403
775
731
208
499
975
949
936
545
203
869
161
85
636
52
345
350
548
546
383
774
421
861
297
562
868
29
680
372
363
702
419
30
545
514
600
682
309
767
364
137
986
687
91
BubbleSort - 0 steps
355
951
136
788
306
332
131
924
821
260
150
266
752
723
98
311
86
651
755
125
336
416
648
608
928
804
520
857
304
749
419
577
326
410
141
500
386
973
548
328
28
198
105
268
434
615
397
492
668
483
564
19
286
806
771
458
787
297
731
990
532
233
564
758
4
491
669
141
878
740
8
153
736
421
532
194
846
522
855
573
216
511
689
565
22
265
273
605
865
119
539
665
704
6
221
655
835
363
976
449
InsertionSort - 0 steps
48
560
499
685
90
197
825
226
374
215
605
729
465
580
618
600
385
270
220
753
711
509
918
724
792
138
324
68
866
78
568
676
438
666
978
26
369
830
469
732
742
21
7
834
689
246
835
831
204
935
31
104
481
885
134
529
870
555
572
712
502
439
980
180
866
378
513
627
891
662
221
437
479
175
222
659
384
353
409
203
421
218
604
523
427
417
878
403
239
590
396
179
859
582
932
148
248
878
846
250
ShellSort - 0 steps
472
928
418
446
627
661
305
281
776
118
495
184
976
588
407
919
46
294
855
56
322
570
698
624
326
654
5
894
826
647
176
394
808
843
784
927
231
353
38
317
849
895
384
794
336
772
32
519
877
339
127
678
269
695
758
943
416
301
893
978
542
192
567
650
404
152
104
615
928
622
347
220
686
112
730
84
79
462
610
144
147
216
909
201
231
666
519
51
668
610
622
689
854
286
194
298
636
984
449
454
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