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.

230
855
589
436
686
720
533
304
935
399
47
766
234
261
796
111
60
561
987
666
775
944
956
270
802
575
555
615
918
979
890
27
683
359
12
775
911
720
417
215
223
683
510
754
74
502
210
943
983
136
69
198
91
715
963
668
218
879
103
161
839
643
873
5
972
32
436
774
792
205
59
449
309
496
513
324
505
854
250
883
381
930
851
943
212
824
267
231
529
453
16
211
167
286
64
928
828
65
371
99
BubbleSort - 0 steps
333
729
874
353
323
481
402
544
773
382
928
603
371
560
453
972
461
26
793
100
137
289
71
183
837
440
911
771
548
7
954
998
185
793
808
206
971
208
93
353
853
365
830
527
843
535
304
420
567
814
697
104
443
702
181
266
681
218
918
79
523
630
423
730
459
9
359
392
1000
776
430
806
207
741
730
158
54
854
662
614
731
733
447
988
167
514
478
901
807
939
734
606
158
512
281
194
285
946
668
523
InsertionSort - 0 steps
877
686
651
347
133
909
776
527
322
853
717
322
197
496
825
471
983
94
906
488
308
438
470
861
234
344
579
337
494
125
127
853
225
818
654
860
97
927
706
712
767
660
231
354
972
2
310
859
438
373
787
759
342
934
260
731
817
277
244
55
53
326
200
1
47
779
827
718
967
711
839
177
508
409
617
174
450
329
901
771
947
85
425
782
678
175
463
132
988
515
243
407
963
396
739
542
171
724
927
277
ShellSort - 0 steps
223
267
885
953
94
819
174
250
707
43
35
210
980
961
705
518
190
191
725
114
980
239
387
22
605
130
458
514
277
450
310
636
377
306
786
72
979
685
252
620
142
307
526
660
141
846
38
530
186
728
7
477
361
91
454
964
108
989
525
280
985
423
372
734
17
287
236
950
781
610
945
445
437
808
992
711
818
414
2
196
974
301
288
628
860
492
423
450
200
825
133
709
827
89
361
318
689
891
28
739
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