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.

59
353
855
680
192
608
977
83
458
914
130
652
312
125
994
681
561
274
312
374
590
781
292
959
254
372
807
772
134
148
525
106
898
639
279
480
854
638
64
517
883
529
692
787
256
455
135
443
186
501
668
994
824
114
158
487
399
366
942
520
823
770
150
514
34
576
292
347
194
584
94
629
499
228
510
664
868
304
880
669
882
913
132
534
881
502
702
263
493
945
396
965
995
540
283
127
603
890
308
424
BubbleSort - 0 steps
10
667
511
774
752
20
158
63
582
969
439
578
384
892
827
467
57
97
918
280
174
748
217
3
557
692
898
911
982
693
949
287
27
265
139
340
276
12
687
57
320
297
968
317
129
63
285
955
295
177
466
106
402
548
77
334
207
83
782
887
182
287
538
941
769
529
700
683
661
229
730
752
423
309
393
361
892
514
579
520
42
518
246
553
740
754
587
228
105
684
407
883
525
584
71
237
45
299
816
500
InsertionSort - 0 steps
854
323
588
753
729
877
821
587
960
904
524
987
28
311
644
397
59
3
860
230
833
57
636
726
550
306
599
25
508
323
689
205
383
524
728
603
656
226
262
1000
731
456
35
367
357
286
493
405
720
423
621
38
254
166
782
122
861
27
130
234
335
477
993
498
882
586
585
831
158
535
536
512
986
478
152
596
819
464
34
524
95
225
332
834
847
59
670
1000
205
786
989
368
265
451
183
983
10
881
59
955
ShellSort - 0 steps
997
995
651
686
591
519
529
94
605
908
8
899
356
558
61
387
339
800
522
550
776
368
846
230
5
659
269
352
189
264
534
133
931
50
775
547
73
770
75
939
652
324
713
692
37
1
637
919
445
321
964
960
275
294
815
634
171
191
574
198
498
665
507
758
818
411
781
438
679
299
682
196
61
254
8
920
237
144
131
353
186
261
613
74
148
287
969
670
877
23
200
875
341
17
521
261
381
424
31
82
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