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.

432
112
90
829
398
892
649
315
35
168
442
849
44
977
122
758
755
256
609
539
288
837
299
500
237
310
738
204
444
455
59
376
374
103
247
808
954
628
58
428
387
790
874
848
586
120
57
541
639
44
884
319
903
666
629
477
993
779
78
490
319
148
639
776
408
72
318
395
537
565
912
489
737
838
436
894
375
53
97
834
523
472
882
454
98
958
151
720
906
328
569
217
575
319
585
420
99
208
42
14
BubbleSort - 0 steps
132
138
721
770
590
273
724
377
41
651
746
397
362
770
369
144
883
16
25
346
317
111
438
281
825
387
589
234
581
658
870
747
964
871
169
648
912
428
959
186
649
675
460
253
430
698
769
425
766
61
306
147
169
118
751
705
920
981
341
37
114
726
673
432
871
288
528
59
346
173
660
828
556
562
415
407
346
829
521
787
454
931
177
519
17
440
170
613
2
100
402
577
51
112
425
127
705
809
398
608
InsertionSort - 0 steps
493
179
385
717
885
4
691
561
447
964
811
798
848
734
746
275
435
146
188
736
711
904
380
936
236
210
879
356
988
312
827
636
897
809
130
442
640
305
13
868
710
913
762
403
624
280
909
689
704
937
178
845
980
415
168
898
470
457
958
456
263
721
674
65
47
432
656
855
428
450
970
981
812
732
956
835
304
614
854
107
628
74
968
171
562
203
487
116
150
230
904
481
448
983
107
299
27
68
639
348
ShellSort - 0 steps
905
267
82
971
819
163
448
909
307
671
992
856
248
624
790
651
260
519
633
965
525
937
478
165
157
719
821
526
541
748
442
542
770
425
761
538
602
563
74
401
492
713
900
693
964
507
153
776
509
132
414
487
66
120
648
99
791
442
588
344
266
700
850
134
716
231
431
586
870
790
144
938
236
867
440
305
610
739
15
240
625
244
500
152
737
746
172
576
338
953
822
7
132
112
238
355
650
583
900
302
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