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.

84
356
938
211
789
881
379
468
663
422
995
564
887
46
971
640
137
758
954
637
934
586
333
476
969
471
571
777
84
240
412
421
56
198
903
563
839
692
751
395
540
43
569
657
781
421
656
782
650
886
534
273
981
675
276
496
594
159
127
820
909
686
460
623
440
565
764
947
606
37
14
542
975
928
116
860
251
770
874
509
419
50
370
807
674
31
350
396
797
708
50
342
43
767
498
431
236
605
368
411
BubbleSort - 0 steps
495
897
94
316
935
806
773
758
242
275
664
529
835
616
939
465
472
366
667
348
134
139
44
204
341
109
57
327
277
350
840
658
452
216
755
410
907
93
668
688
87
229
733
230
141
996
231
892
376
174
301
311
222
930
58
923
214
951
479
366
32
203
255
296
246
610
287
339
546
968
246
163
456
739
51
913
824
733
33
977
302
79
249
900
142
712
558
252
245
930
694
251
177
800
329
149
938
879
981
279
InsertionSort - 0 steps
912
239
875
802
119
140
172
922
97
749
774
809
689
836
14
659
265
166
575
380
137
209
549
669
63
296
937
644
833
975
301
854
957
641
587
180
108
919
642
374
957
291
970
271
633
807
943
815
120
217
513
138
261
984
864
561
379
965
852
235
201
877
493
800
67
795
738
566
412
242
751
452
898
453
495
888
24
813
731
921
949
89
921
610
113
267
421
896
457
466
858
401
473
120
132
566
521
202
263
843
ShellSort - 0 steps
282
495
157
911
282
146
295
31
237
341
648
909
214
946
70
935
581
602
56
296
632
259
733
367
987
133
209
287
802
800
404
84
852
260
449
913
516
437
158
328
454
276
450
445
248
716
768
609
973
854
907
93
190
12
871
929
830
971
26
435
671
209
814
222
109
205
685
912
413
792
550
452
779
11
607
472
484
843
562
588
531
793
340
532
625
73
353
292
410
746
632
259
360
815
442
636
263
155
387
751
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