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.

319
776
494
532
529
822
71
560
863
194
328
409
632
952
987
841
629
541
654
177
427
727
417
819
165
117
468
742
18
945
902
480
687
136
16
638
289
22
561
274
123
238
683
127
419
150
671
486
405
239
734
44
309
703
353
586
896
888
937
907
840
581
617
973
823
934
596
820
685
685
1
240
829
924
652
342
842
141
895
11
998
841
457
573
680
697
666
146
829
70
941
224
457
24
984
267
618
379
544
767
BubbleSort - 0 steps
399
827
134
683
463
468
632
695
287
22
745
486
130
728
557
505
545
735
498
748
704
466
785
813
716
112
637
884
325
652
849
156
261
816
317
158
596
946
266
775
487
139
904
483
428
127
902
32
701
953
310
256
211
744
547
791
779
493
954
885
780
776
650
682
405
785
354
29
148
723
26
273
171
570
438
710
157
795
887
168
224
440
999
503
366
423
7
948
579
217
861
394
887
316
343
673
815
30
924
133
InsertionSort - 0 steps
136
788
6
327
624
161
745
916
437
663
681
682
978
860
393
566
619
857
987
663
130
558
547
196
457
82
479
165
980
222
385
878
951
960
968
172
95
692
455
609
643
672
551
200
312
758
720
213
919
873
848
883
197
763
794
315
549
702
988
161
289
226
239
267
328
386
164
439
612
505
337
66
728
579
651
691
39
918
227
532
329
325
211
289
499
175
845
889
530
919
984
295
151
549
936
647
311
479
563
44
ShellSort - 0 steps
848
835
287
559
394
928
872
157
993
519
340
451
497
773
723
857
168
282
91
876
792
41
149
303
398
621
319
446
726
33
855
579
586
685
730
862
827
546
413
418
498
760
564
259
668
293
561
710
674
922
514
917
311
222
297
697
386
646
2
621
335
467
134
18
395
864
405
739
313
957
768
184
327
788
653
175
932
4
74
224
66
388
631
414
822
584
619
313
421
532
307
161
408
740
836
918
319
104
567
589
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