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.

190
59
381
580
739
815
973
825
869
575
556
924
11
394
904
201
495
736
676
265
333
401
843
403
835
209
496
984
488
720
938
668
13
87
563
76
930
839
652
993
523
462
556
228
31
638
882
458
555
742
431
711
367
822
54
58
94
832
238
532
352
775
973
206
791
615
280
194
287
826
30
957
238
712
771
773
390
554
105
904
11
39
761
36
86
787
75
670
778
918
715
96
472
48
272
624
323
83
649
736
BubbleSort - 0 steps
385
922
95
179
204
697
892
287
27
969
424
375
812
51
394
910
861
247
79
766
597
661
129
143
584
966
798
71
608
274
126
84
236
251
402
787
269
539
319
156
451
727
779
385
592
789
32
790
826
457
418
580
938
951
188
395
957
898
330
497
86
670
596
905
942
783
180
967
213
889
510
925
560
648
773
307
271
8
322
725
528
349
886
747
437
386
593
23
744
816
905
656
437
118
472
195
284
252
694
796
InsertionSort - 0 steps
826
441
668
355
981
326
989
687
873
372
461
768
337
71
757
655
940
120
410
711
554
124
241
62
674
696
714
173
154
276
678
195
430
580
515
328
180
798
595
200
845
949
580
711
635
844
324
109
101
630
415
737
740
988
597
749
901
136
80
794
879
119
764
604
178
780
333
469
378
344
89
722
250
117
988
780
435
414
569
299
396
99
596
171
672
571
665
532
179
96
743
254
326
733
956
359
796
331
650
222
ShellSort - 0 steps
176
852
904
339
364
171
92
119
244
439
773
603
787
227
574
538
420
828
568
108
4
532
355
895
393
798
122
544
70
139
207
439
542
447
951
179
548
840
757
482
172
294
104
317
293
331
360
675
73
82
764
398
375
450
307
459
362
102
669
796
891
709
696
596
954
605
628
134
708
891
567
562
887
663
16
742
700
197
751
63
178
647
651
863
849
343
846
919
366
401
587
340
153
543
72
857
217
286
748
931
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