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.

937
263
219
666
109
696
700
854
209
821
516
53
43
447
535
898
5
869
868
755
870
240
1000
571
165
958
731
986
994
330
782
732
541
196
924
151
912
661
708
78
13
758
531
247
330
723
111
147
696
488
261
21
31
896
22
506
363
111
834
420
782
856
873
996
894
641
663
866
557
940
306
603
164
634
501
277
476
386
139
769
774
448
585
989
862
836
544
933
332
305
595
307
133
122
579
851
913
397
900
898
BubbleSort - 0 steps
823
848
776
989
470
649
613
47
750
196
487
894
956
203
354
805
740
11
139
341
1
960
173
422
331
477
657
244
676
990
123
415
115
132
812
435
122
411
657
494
678
708
562
87
479
415
155
170
782
743
299
529
429
778
498
143
996
846
858
785
569
816
320
131
446
39
849
41
96
174
539
387
465
477
809
786
355
341
440
831
174
369
910
734
187
527
583
300
200
761
972
636
222
123
621
897
168
91
630
666
InsertionSort - 0 steps
543
92
557
441
399
809
420
901
53
475
818
857
347
970
785
903
915
479
813
583
700
476
507
345
357
770
648
938
675
655
524
905
466
381
470
511
103
189
233
315
603
634
8
388
336
389
838
118
188
507
719
570
853
333
923
271
734
332
742
802
127
677
152
329
788
491
106
670
668
999
413
274
295
517
728
246
637
763
927
865
225
63
776
888
658
621
940
763
656
706
375
883
370
254
203
702
37
670
335
918
ShellSort - 0 steps
718
661
522
404
926
5
465
408
140
60
302
585
773
283
167
445
443
185
84
492
34
32
740
520
238
109
90
964
734
89
524
199
134
991
435
202
261
248
202
89
296
659
428
610
108
764
741
595
74
466
851
865
499
839
179
413
42
137
174
903
763
419
193
174
333
471
224
635
466
219
673
687
279
517
408
851
786
887
679
862
996
685
907
212
309
975
545
393
993
56
586
837
780
857
290
731
54
115
448
267
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