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.

17
444
592
119
450
29
652
284
137
659
236
363
522
500
862
503
538
177
785
631
217
103
987
157
486
52
777
20
723
705
408
715
592
383
584
360
990
22
8
55
217
754
867
66
134
287
984
736
570
244
183
2
319
968
256
807
220
791
571
898
76
831
548
746
99
697
105
202
97
868
911
487
132
1
322
133
402
851
385
88
118
125
354
423
108
275
386
472
552
407
559
222
619
307
571
557
105
453
440
265
BubbleSort - 0 steps
137
603
888
736
425
321
916
652
525
330
167
376
115
818
756
895
339
819
469
133
371
535
801
971
181
148
387
354
951
333
380
142
566
615
464
272
156
296
756
522
845
969
320
457
11
791
590
593
704
835
40
638
532
443
744
240
17
940
632
526
707
280
113
428
41
729
557
562
654
833
961
505
829
810
511
847
210
536
477
189
720
424
717
547
435
363
912
330
981
619
900
152
742
660
762
445
919
117
275
73
InsertionSort - 0 steps
160
447
814
848
501
246
590
518
481
866
602
178
625
223
148
75
910
455
112
493
452
885
650
92
393
313
527
346
167
292
484
927
870
811
583
180
762
15
102
920
121
888
419
989
31
469
178
150
405
366
782
728
236
743
590
114
562
164
164
131
989
495
646
593
431
734
644
747
630
766
956
6
454
32
468
86
835
549
137
828
964
928
523
251
200
301
967
920
811
537
590
651
976
82
462
264
999
156
338
968
ShellSort - 0 steps
713
270
803
821
638
899
719
435
990
847
602
284
75
203
662
571
725
148
327
778
748
200
964
155
907
188
851
138
217
373
376
368
560
460
410
264
717
256
935
277
329
391
795
950
135
334
91
785
323
174
525
883
525
399
751
417
237
265
776
194
522
475
372
371
611
550
970
760
309
174
891
435
462
833
675
51
396
198
684
669
33
651
262
156
243
849
919
606
320
184
839
816
288
696
36
922
291
877
739
337
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