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.

415
767
636
974
972
48
950
833
494
346
782
139
441
162
307
101
786
436
611
218
775
847
461
25
366
385
823
446
3
838
322
844
339
234
501
837
689
796
379
375
96
891
717
35
480
63
529
480
150
948
735
564
429
621
157
440
160
509
880
912
765
175
682
714
120
742
75
889
940
196
242
489
964
823
511
639
432
574
102
982
959
789
860
524
774
961
115
371
369
196
498
96
384
49
962
715
950
535
645
478
BubbleSort - 0 steps
677
571
679
564
725
355
748
949
844
196
928
492
180
158
628
46
142
549
354
993
96
859
114
933
407
437
791
821
825
746
436
575
501
486
297
713
403
496
102
583
216
750
986
808
562
500
464
477
661
921
451
984
108
807
83
512
616
508
291
759
229
835
399
365
612
900
357
410
132
185
963
531
979
766
823
421
742
155
62
808
768
61
505
732
544
849
110
986
754
714
606
682
832
269
536
499
44
870
601
319
InsertionSort - 0 steps
571
981
888
681
279
232
173
590
423
418
307
286
54
592
211
521
376
992
452
970
240
633
489
929
710
500
895
161
290
491
137
146
354
38
912
892
856
510
133
924
386
818
684
52
824
528
998
757
312
196
282
378
817
957
997
306
341
296
786
948
876
594
579
782
68
767
476
132
300
389
965
878
812
547
851
989
607
494
87
807
445
675
601
527
725
187
670
645
133
994
415
492
156
532
219
572
957
25
45
248
ShellSort - 0 steps
802
216
741
584
344
218
612
264
406
227
394
799
561
63
151
171
100
118
561
684
714
749
509
147
568
12
351
252
389
698
642
812
52
280
665
494
892
757
526
389
373
682
687
607
719
909
992
509
822
938
983
352
823
225
453
125
5
662
591
113
435
977
651
161
410
404
148
104
53
61
882
277
485
824
158
704
72
564
402
619
802
30
991
565
212
334
505
547
374
766
561
546
55
790
306
491
9
544
570
39
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