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.

885
192
2
202
995
779
913
347
695
930
480
106
474
833
271
391
128
623
371
887
87
910
216
948
287
7
503
466
77
260
318
886
345
802
352
132
877
647
118
197
684
568
244
775
186
475
803
440
347
729
458
432
39
746
943
969
507
825
919
235
247
119
977
627
407
669
893
449
216
723
512
805
133
903
337
247
527
482
904
982
698
133
739
444
409
405
324
66
82
343
86
929
276
76
89
443
321
823
658
709
BubbleSort - 0 steps
230
423
947
339
132
344
180
702
233
495
546
203
463
970
661
417
913
268
435
905
702
267
437
992
8
232
442
388
708
699
133
953
211
52
563
904
936
26
572
839
968
674
616
712
502
377
107
488
25
846
481
949
494
733
285
551
943
711
98
714
425
150
197
840
506
999
815
299
134
644
327
39
473
773
255
643
35
721
293
205
425
350
382
49
389
504
513
992
638
47
719
856
930
472
746
764
446
575
948
456
InsertionSort - 0 steps
289
603
53
237
744
399
194
224
698
86
400
293
300
171
495
157
765
760
244
224
596
670
751
960
490
165
205
806
982
609
468
58
470
496
270
79
543
37
832
148
989
87
558
65
673
526
200
310
50
849
81
552
375
517
162
421
473
839
712
551
867
476
571
701
217
553
952
317
675
482
874
145
148
644
606
89
797
83
462
719
744
400
98
350
558
254
782
435
79
470
288
537
567
796
100
82
154
552
654
125
ShellSort - 0 steps
790
247
836
561
345
633
948
37
531
413
359
51
799
555
792
238
45
206
31
854
322
818
532
74
795
176
340
515
1
535
348
616
728
1
810
639
933
846
273
245
416
998
314
741
460
976
275
595
711
910
263
724
765
637
837
91
151
147
28
283
457
645
574
919
441
46
620
652
488
790
985
585
701
931
659
899
689
455
592
438
77
52
182
724
617
804
393
698
204
37
43
331
675
402
192
800
704
735
919
631
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