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.

232
703
212
455
427
136
752
46
713
708
803
881
550
741
483
432
79
844
24
410
508
248
335
135
609
961
889
225
478
259
590
68
20
668
101
489
289
574
351
855
929
940
795
838
445
240
357
288
882
340
81
214
998
826
111
217
46
96
580
938
632
492
14
456
765
699
995
327
459
297
247
415
584
381
104
442
623
735
423
591
463
940
867
901
982
172
211
919
341
674
776
151
373
785
584
79
484
270
209
338
BubbleSort - 0 steps
951
187
442
411
702
641
492
669
815
243
947
294
409
402
680
338
789
153
742
916
275
242
118
687
130
666
515
687
422
297
692
547
502
409
545
775
577
309
136
97
705
594
262
616
720
361
677
810
190
288
323
942
16
91
752
545
207
208
595
859
403
394
526
567
69
417
800
474
152
882
700
182
617
220
825
90
109
644
788
242
667
576
327
612
173
480
243
307
241
89
847
323
395
115
918
497
202
643
816
437
InsertionSort - 0 steps
964
7
837
230
404
128
447
170
651
572
34
389
395
974
488
137
550
295
297
37
686
434
677
29
100
246
868
109
982
813
702
240
20
411
832
34
694
933
378
685
247
929
274
92
958
785
793
235
827
843
781
237
447
380
377
401
765
453
752
975
630
299
93
671
269
700
539
373
455
105
138
845
977
994
496
131
510
301
278
415
684
780
823
543
437
244
895
994
835
21
726
824
216
73
772
230
869
573
713
900
ShellSort - 0 steps
771
130
503
70
70
699
18
213
880
147
502
389
94
39
146
739
929
116
17
720
871
614
475
214
50
26
56
207
59
742
477
333
760
759
189
333
562
190
725
835
875
408
523
84
926
748
209
904
828
700
123
164
35
859
921
307
550
895
264
32
912
956
511
170
604
629
774
707
365
316
279
91
989
510
623
955
50
440
769
431
215
307
214
900
876
820
954
116
237
500
106
110
643
844
949
449
105
446
799
991
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