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.

178
820
962
132
235
880
38
263
244
204
546
464
902
936
973
94
156
341
404
621
351
771
958
633
336
317
777
533
399
792
217
68
243
272
664
199
237
51
721
239
121
863
536
618
169
592
663
744
143
365
693
660
81
64
540
515
568
533
761
238
112
252
459
344
435
227
330
848
150
126
850
997
638
52
172
966
436
772
485
280
410
608
734
802
437
453
702
204
776
2
266
372
911
862
578
437
835
166
183
521
BubbleSort - 0 steps
111
487
631
354
470
688
138
75
194
510
322
156
196
855
645
714
176
915
553
708
867
443
794
542
260
748
167
415
256
768
799
827
400
41
314
141
359
935
184
785
272
853
583
606
694
11
371
464
533
191
487
832
605
583
355
344
135
413
635
16
528
734
808
544
561
174
654
209
487
618
967
86
868
301
681
881
516
892
88
118
421
866
99
372
615
602
131
403
209
794
367
724
76
965
705
379
835
743
243
863
InsertionSort - 0 steps
469
793
700
90
394
294
479
124
820
564
150
348
629
995
155
671
747
56
799
613
138
195
107
371
867
72
546
503
603
818
20
499
452
320
252
704
806
528
500
71
608
266
242
723
794
699
35
643
926
356
856
602
416
752
397
997
144
187
444
343
174
652
585
344
417
610
297
882
640
306
367
992
218
737
596
508
526
522
520
154
278
188
415
343
830
293
937
434
845
419
878
275
743
518
339
911
174
312
496
791
ShellSort - 0 steps
347
905
576
961
648
559
33
411
969
36
742
705
223
458
334
863
836
302
661
525
582
871
809
220
58
441
665
509
608
807
482
295
881
67
602
129
496
975
854
23
769
494
28
996
522
761
659
937
722
670
958
998
163
808
154
744
106
184
222
792
658
281
109
142
133
985
987
452
545
658
57
214
286
993
663
879
418
510
644
172
855
52
662
693
290
379
504
145
717
21
427
416
229
348
699
389
74
333
836
916
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