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.

640
983
271
603
791
851
326
803
458
636
926
990
903
10
161
996
391
286
620
877
648
28
86
148
194
995
915
824
116
646
901
980
341
388
524
334
948
631
418
477
48
311
529
403
760
681
208
481
30
196
480
549
918
282
418
953
391
693
10
539
45
758
745
887
321
490
963
14
379
574
547
1000
701
915
28
789
519
739
62
668
317
231
827
704
715
700
298
32
557
82
574
276
365
682
992
903
496
95
515
571
BubbleSort - 0 steps
67
907
320
394
429
164
155
801
898
947
787
771
597
685
790
775
853
174
414
283
247
107
794
531
303
524
233
426
1000
956
37
99
954
709
344
932
846
477
715
142
504
653
658
893
938
782
434
199
886
947
838
155
740
116
365
633
910
867
662
423
84
136
944
108
349
357
762
157
435
946
330
259
129
365
508
802
864
222
844
90
138
697
172
60
399
240
533
437
895
574
423
782
358
9
449
698
233
797
41
228
InsertionSort - 0 steps
531
530
710
484
449
294
955
419
630
355
502
127
456
3
990
94
164
497
403
265
194
468
68
748
990
693
910
592
157
802
421
974
730
625
7
943
185
218
26
21
319
783
27
229
991
343
959
249
213
772
877
670
7
282
200
153
847
327
475
55
14
849
482
179
197
232
657
432
113
620
417
759
213
739
913
106
158
783
95
908
900
748
712
952
391
906
671
671
369
644
82
467
757
156
754
390
485
899
851
865
ShellSort - 0 steps
764
273
503
328
857
399
779
922
16
90
325
937
356
886
538
748
261
450
375
980
622
88
95
292
608
786
72
377
372
455
473
573
714
417
216
98
900
976
430
772
644
954
257
887
183
632
274
444
858
16
930
50
482
321
38
564
81
38
860
876
129
405
973
452
596
405
953
761
315
127
917
67
477
825
86
695
498
625
766
467
876
437
569
527
573
380
841
693
715
344
311
293
831
586
776
21
834
237
297
182
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