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.

73
238
481
298
849
288
289
136
925
493
445
206
921
310
477
591
187
362
574
489
51
691
440
922
699
90
455
57
239
410
73
703
878
84
49
595
187
631
654
731
849
243
278
100
225
912
454
53
531
351
796
975
310
782
502
303
737
165
281
772
511
106
775
841
822
251
313
931
774
245
738
910
325
671
592
114
647
707
764
404
527
223
518
511
826
409
594
317
882
690
551
704
256
345
957
926
441
504
640
779
BubbleSort - 0 steps
721
638
526
775
31
690
840
769
608
587
972
989
614
107
752
677
109
515
952
751
181
42
306
832
840
477
907
312
230
135
24
249
666
32
743
131
926
900
186
558
500
591
381
298
806
693
879
978
318
397
795
280
236
235
861
576
507
792
579
286
434
691
725
987
403
947
498
833
794
120
889
48
960
66
968
199
850
990
675
465
640
690
408
386
187
987
571
73
120
333
542
651
527
776
808
734
932
954
459
439
InsertionSort - 0 steps
692
380
653
999
329
465
814
116
225
740
603
55
671
498
718
80
240
478
926
994
866
597
113
612
970
957
227
521
79
625
88
60
651
837
193
928
61
95
830
814
630
243
164
290
391
987
735
110
273
923
467
350
578
552
894
391
711
922
604
722
29
878
823
196
943
912
559
787
643
392
881
899
462
805
961
278
86
409
39
618
218
128
227
188
598
91
902
690
693
453
325
724
819
552
320
314
922
318
93
135
ShellSort - 0 steps
711
720
680
20
243
190
145
256
590
19
545
931
972
286
656
267
264
144
456
410
356
699
227
661
324
114
238
979
822
807
303
217
304
49
635
800
296
471
642
846
478
909
294
219
375
846
777
778
244
476
788
125
803
334
218
166
780
47
295
926
159
920
349
438
407
385
227
506
313
199
283
487
579
193
842
555
165
184
304
372
405
854
41
198
584
196
30
148
486
63
233
439
859
128
848
267
944
836
831
655
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