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.

365
713
264
234
217
808
921
335
616
718
519
270
34
811
337
540
403
637
603
696
854
335
878
934
1000
66
617
682
907
878
131
906
154
269
856
247
220
329
850
354
418
687
22
945
262
145
987
1000
811
175
562
856
336
126
12
718
721
683
76
795
261
443
325
772
754
265
224
604
490
561
163
721
618
947
694
412
507
252
392
91
868
498
813
24
644
563
857
9
902
740
12
309
649
356
451
643
186
107
123
193
BubbleSort - 0 steps
211
445
87
112
907
981
897
405
694
821
804
69
866
964
329
65
783
419
453
914
959
423
935
542
419
163
146
820
955
516
93
712
74
840
246
210
630
519
359
291
987
772
562
271
330
736
481
893
740
112
997
480
58
688
85
256
988
951
201
6
592
432
653
222
107
366
525
62
574
219
159
166
502
827
866
686
930
482
92
540
177
744
856
132
142
647
423
473
478
30
177
35
972
665
330
797
653
658
264
307
InsertionSort - 0 steps
920
900
310
205
124
798
649
166
837
469
92
825
692
547
563
501
10
739
928
487
966
57
686
23
527
125
968
239
324
455
937
724
2
87
10
67
76
988
178
799
539
524
916
263
892
529
213
230
22
5
253
898
853
672
532
893
671
284
803
494
107
840
22
647
656
420
695
441
983
518
428
578
643
769
249
830
929
629
356
564
899
389
870
309
697
763
469
140
525
890
796
317
847
909
236
482
568
679
442
223
ShellSort - 0 steps
261
352
840
10
322
481
822
878
289
561
986
246
440
919
766
10
341
895
565
575
870
494
276
928
267
108
814
868
741
92
592
205
939
252
706
52
302
176
251
586
566
480
137
848
46
915
413
191
839
71
704
853
618
955
187
743
437
396
522
905
464
718
971
155
468
456
366
821
466
814
807
741
898
655
196
992
792
375
165
189
287
501
344
253
733
645
894
718
776
821
844
696
166
181
547
998
852
652
326
235
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