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.

331
584
301
881
656
731
639
457
320
557
539
627
484
187
315
445
585
399
905
618
556
239
356
233
712
277
824
390
742
584
789
677
677
96
66
638
11
592
384
241
41
139
911
59
891
64
664
505
533
525
69
196
489
700
980
124
348
75
962
954
783
126
767
166
138
652
460
453
280
125
509
238
866
54
100
464
39
609
269
970
68
955
737
971
17
521
889
908
167
808
541
482
872
389
862
295
58
862
262
431
BubbleSort - 0 steps
538
106
793
776
453
826
872
140
91
122
395
715
301
527
163
241
460
367
240
6
852
588
902
395
664
751
485
241
474
446
440
70
524
17
233
505
983
630
898
417
672
154
239
490
959
582
674
321
238
986
871
367
772
888
942
566
696
143
937
220
625
784
947
606
156
527
890
106
717
68
46
694
310
586
802
306
98
65
400
758
787
66
585
855
708
96
460
354
884
525
726
471
985
320
308
225
326
53
321
97
InsertionSort - 0 steps
105
535
613
11
399
257
300
779
364
319
280
764
25
606
758
654
512
854
99
155
616
366
981
628
624
229
299
897
350
604
835
247
441
929
169
602
91
788
153
723
639
409
272
267
475
879
962
635
810
748
213
211
666
132
821
163
148
485
773
543
127
547
945
671
534
376
830
293
369
725
767
937
186
403
58
133
611
856
871
990
137
826
379
627
682
673
611
656
377
409
3
480
123
156
321
622
290
108
197
454
ShellSort - 0 steps
999
642
560
521
530
753
124
608
321
957
574
546
667
863
488
568
144
599
298
799
670
74
780
162
86
680
67
490
430
247
3
799
776
469
705
754
548
838
942
633
224
674
163
158
7
990
395
20
424
286
244
221
888
201
201
760
782
238
254
390
94
909
266
953
718
653
891
606
324
351
193
781
719
997
449
860
538
456
891
922
149
209
160
353
647
936
221
208
1
959
580
610
764
576
414
74
466
182
928
532
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