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.

140
657
481
683
737
228
577
379
444
68
953
577
208
592
573
874
935
70
411
487
162
95
806
237
962
566
652
569
529
753
914
496
699
63
222
621
814
258
452
844
862
647
235
95
430
34
280
289
437
240
115
108
155
488
429
741
419
547
346
798
252
365
843
140
141
455
836
581
530
773
998
413
759
227
863
468
967
945
604
1
874
557
351
285
591
3
497
752
437
819
79
471
878
535
196
544
68
561
519
985
BubbleSort - 0 steps
384
361
621
45
294
50
250
186
444
291
70
619
760
539
796
978
673
881
26
143
163
381
899
668
420
226
132
740
632
860
928
82
379
794
943
582
1000
81
550
419
438
693
521
389
770
943
354
80
541
713
115
210
571
948
154
646
103
990
856
272
254
153
868
599
449
987
758
592
425
809
76
304
538
57
429
562
931
193
555
621
933
97
35
281
703
809
374
930
608
907
18
48
539
384
333
542
290
882
65
82
InsertionSort - 0 steps
294
591
348
387
12
427
717
993
364
363
489
799
782
15
602
875
42
704
824
308
211
189
630
39
583
770
401
416
590
137
558
653
860
957
458
797
23
416
472
178
953
88
541
970
113
353
825
258
325
601
646
54
971
122
508
133
714
415
980
144
793
620
321
30
704
948
692
815
840
905
874
673
695
876
60
817
604
941
550
294
489
720
236
167
237
438
501
980
649
575
459
188
31
722
550
462
276
159
536
428
ShellSort - 0 steps
986
621
827
354
484
183
178
85
190
123
449
686
498
853
410
646
123
943
711
884
406
705
693
836
551
430
75
241
826
737
914
592
719
996
277
840
419
263
766
370
483
660
113
881
772
321
669
10
134
958
715
732
279
261
391
113
403
40
846
865
827
412
737
492
553
61
729
716
557
574
798
304
178
347
11
220
674
948
80
957
77
540
395
682
826
72
941
674
424
879
62
943
393
685
577
122
114
981
262
742
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