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.

451
921
841
853
860
136
447
327
949
898
863
375
849
11
550
700
225
328
604
921
199
246
499
796
449
113
816
124
509
936
800
227
254
581
97
255
300
170
482
926
416
84
951
9
720
235
477
563
761
686
984
648
990
976
697
297
703
834
551
918
932
516
102
25
456
23
515
740
56
582
892
265
989
344
352
279
471
913
26
440
141
489
890
52
94
621
750
617
221
107
71
202
583
117
517
235
233
948
923
482
BubbleSort - 0 steps
997
343
447
483
125
94
463
396
768
914
150
897
388
790
95
795
22
673
536
45
195
485
508
986
101
628
778
435
916
86
20
378
832
728
631
119
851
760
447
382
125
651
236
606
394
455
923
281
863
574
440
420
529
695
93
436
358
38
671
694
238
310
992
48
130
758
188
323
685
825
9
472
893
568
870
544
469
76
315
273
299
27
254
969
782
869
779
716
458
56
957
552
317
464
754
532
372
611
466
498
InsertionSort - 0 steps
249
286
624
836
979
168
488
132
591
672
80
192
319
932
500
589
297
432
474
55
414
800
997
943
19
677
501
851
182
512
553
677
991
743
773
864
917
52
384
336
751
331
166
577
892
564
53
539
861
938
103
637
931
675
957
632
97
762
643
182
640
301
248
302
572
191
603
302
843
508
346
114
85
773
425
778
309
370
592
500
319
661
56
5
550
440
574
320
288
996
69
932
475
127
906
924
927
508
453
886
ShellSort - 0 steps
463
905
237
74
532
514
272
572
176
213
120
789
891
107
296
799
816
564
637
663
143
255
489
184
923
224
61
298
832
560
417
759
674
726
703
89
493
830
682
15
196
14
701
302
79
548
501
941
942
685
279
116
989
200
862
832
29
337
626
640
859
422
604
43
814
818
206
633
556
505
343
718
562
596
149
79
360
103
798
911
70
266
470
158
893
234
172
45
404
938
231
374
839
998
554
228
78
728
452
542
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