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.

637
699
362
800
445
30
526
130
898
155
30
579
725
18
174
376
772
225
139
947
69
815
499
431
958
872
543
873
299
786
948
356
674
317
233
26
553
956
212
810
334
399
907
276
958
478
761
349
317
336
455
657
711
342
981
716
402
425
90
538
480
744
772
445
966
850
857
527
966
26
792
832
483
267
464
240
589
185
187
781
256
384
411
463
447
248
38
821
178
529
999
514
233
266
152
921
72
147
597
967
BubbleSort - 0 steps
104
781
813
314
494
450
624
294
521
480
956
481
264
497
508
113
705
625
135
898
246
141
914
642
713
15
421
476
919
823
559
58
940
515
278
504
553
720
207
269
884
921
992
607
680
465
173
32
603
398
765
617
791
674
142
278
954
888
526
992
985
498
711
959
397
247
79
31
274
51
76
36
487
651
706
901
805
130
53
851
703
170
114
762
966
984
968
836
855
158
447
524
170
999
821
749
839
39
11
833
InsertionSort - 0 steps
170
24
451
171
125
653
306
915
609
279
309
342
933
513
499
353
878
510
415
392
349
81
226
487
268
963
562
119
77
996
364
902
822
315
952
323
747
524
199
89
114
453
811
787
184
279
79
248
133
199
5
941
598
50
313
365
59
977
416
468
47
400
97
743
934
716
416
872
895
412
731
725
967
683
540
956
817
925
816
487
883
3
502
410
347
532
633
678
250
993
56
984
148
647
568
976
902
589
335
410
ShellSort - 0 steps
936
113
702
825
201
143
193
390
965
966
810
598
395
317
421
558
249
849
664
504
903
261
206
854
258
309
731
623
128
521
232
611
246
504
429
103
374
323
203
98
925
444
681
628
735
109
125
55
872
76
744
127
684
242
14
757
368
418
701
129
100
92
140
680
283
449
141
191
165
740
919
274
775
366
980
430
735
706
115
513
468
759
350
842
385
929
129
568
819
459
810
41
5
997
140
794
741
135
856
188
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