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.

24
660
134
338
699
114
356
197
659
444
173
826
147
555
610
372
38
202
66
967
876
450
789
816
672
49
388
952
512
48
533
275
71
399
100
253
373
680
738
217
69
572
940
224
755
62
284
556
261
926
264
828
14
140
725
798
146
517
705
822
983
888
531
901
679
574
994
460
860
214
431
316
453
558
372
152
790
356
823
594
561
280
537
413
126
467
481
722
256
18
68
484
577
425
907
209
27
242
280
763
BubbleSort - 0 steps
476
765
151
269
809
216
847
759
382
368
311
297
637
700
576
211
131
482
937
32
453
278
724
186
457
359
626
516
970
994
147
54
490
216
596
527
397
26
306
745
840
188
991
19
813
257
112
792
808
138
608
773
422
951
929
383
33
303
259
744
471
457
699
645
84
254
361
449
485
263
533
875
302
301
839
894
27
580
544
147
519
158
215
811
937
250
719
80
927
2
78
409
935
429
146
483
299
366
293
726
InsertionSort - 0 steps
600
389
209
384
769
828
322
787
301
766
661
583
550
474
147
36
8
480
41
328
63
169
317
93
217
149
8
861
243
501
814
705
330
215
225
895
235
944
685
645
454
196
716
595
397
382
960
381
539
760
430
880
838
293
107
514
688
212
179
61
455
112
827
795
337
914
123
129
195
775
479
40
746
189
659
658
359
792
392
387
615
966
212
433
816
340
300
228
517
667
519
355
367
906
486
933
582
237
422
512
ShellSort - 0 steps
127
433
256
678
145
740
567
305
629
174
714
222
982
693
886
461
696
923
826
567
813
860
362
174
254
153
558
831
39
152
538
799
991
566
849
375
368
746
439
772
530
929
400
877
181
210
797
231
235
938
32
833
707
953
129
558
914
933
572
32
274
866
326
227
738
664
110
28
762
682
289
300
204
133
423
167
377
354
154
447
811
715
542
771
648
626
304
57
726
294
16
459
166
312
213
818
217
748
164
544
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