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.

655
543
551
901
413
830
789
743
27
188
969
899
313
376
527
147
209
145
898
801
527
449
748
385
359
646
292
627
630
228
828
556
736
701
871
704
532
871
328
905
687
103
366
324
980
624
913
12
751
553
850
332
869
831
132
170
892
421
517
485
44
692
937
792
177
181
118
862
148
318
387
501
923
406
363
977
518
161
481
779
31
546
42
276
233
180
607
689
181
47
733
66
940
241
797
400
709
670
140
936
BubbleSort - 0 steps
698
616
247
866
543
782
671
308
935
976
645
413
215
801
316
375
986
803
354
835
174
539
437
847
421
387
872
493
611
944
519
485
275
44
806
664
483
134
558
445
838
46
1000
85
406
388
130
265
738
972
275
436
319
225
312
537
446
113
193
856
958
320
583
939
745
68
656
18
845
13
605
808
948
647
856
10
459
825
984
482
642
479
798
921
801
650
930
293
915
144
776
990
122
155
481
98
169
750
88
538
InsertionSort - 0 steps
824
796
47
510
698
843
572
582
563
236
558
902
552
518
420
608
802
207
627
74
215
267
465
970
777
208
811
797
767
69
97
763
320
415
17
368
415
539
280
28
939
206
633
754
919
232
149
479
997
316
500
850
858
17
219
197
755
48
360
511
941
388
358
220
669
863
241
365
429
526
403
200
234
47
652
294
960
660
723
306
416
923
884
160
310
327
505
157
66
641
911
979
218
232
373
539
305
531
34
962
ShellSort - 0 steps
154
190
996
580
235
259
788
967
192
919
582
653
575
266
87
437
478
507
545
984
568
797
990
602
329
403
533
779
638
952
858
619
408
732
146
935
353
456
42
460
524
810
512
496
674
448
607
26
731
466
291
106
378
323
861
927
286
123
33
151
760
902
341
510
218
259
411
426
103
183
438
305
539
944
856
989
394
32
802
89
787
683
690
334
496
487
745
545
141
70
10
188
43
684
818
932
265
104
389
978
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