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.

884
348
195
236
633
906
115
161
366
711
904
137
608
313
210
898
423
758
785
272
276
723
329
991
764
472
876
904
954
790
70
718
660
168
883
59
134
948
482
131
180
633
58
766
571
2
957
305
32
795
243
333
54
503
256
466
102
326
890
237
639
188
476
882
595
509
539
249
366
231
959
36
405
275
419
234
18
134
334
205
940
294
9
142
680
307
917
955
993
216
200
345
571
369
814
403
576
753
203
970
BubbleSort - 0 steps
748
260
637
74
621
986
554
812
23
814
422
547
494
698
557
527
185
224
651
354
637
218
685
433
546
336
137
979
715
753
491
938
200
132
971
908
693
327
689
304
45
95
946
992
371
546
691
118
243
37
157
38
718
220
124
505
779
470
775
576
565
850
339
270
249
168
455
111
800
673
149
904
279
232
524
162
472
252
508
319
184
233
820
553
494
60
445
356
131
355
798
145
348
34
957
601
217
508
512
678
InsertionSort - 0 steps
94
854
223
516
991
664
931
674
420
74
962
847
618
958
58
276
439
846
904
417
877
584
910
243
247
126
106
434
66
374
948
770
714
581
831
523
812
443
599
370
994
977
536
260
955
190
93
693
422
646
142
822
227
947
766
93
581
568
874
623
684
781
313
202
810
713
293
939
234
241
323
497
772
541
571
241
141
427
518
353
310
608
159
861
189
309
295
886
547
808
30
269
152
238
629
130
779
816
799
214
ShellSort - 0 steps
951
26
853
759
77
374
273
234
610
265
172
864
699
130
541
204
871
277
20
920
576
997
722
849
599
481
385
855
682
423
516
37
435
971
257
371
41
791
936
426
453
328
689
271
922
174
903
521
872
75
884
207
906
934
792
556
370
27
901
350
205
693
256
324
896
647
609
972
43
950
776
948
966
479
77
873
496
714
321
591
716
606
280
73
997
129
96
539
266
708
883
950
47
551
428
51
556
244
977
530
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