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.

219
89
733
81
936
198
421
408
28
810
612
132
743
156
941
709
23
520
853
506
250
514
816
752
739
166
863
257
532
638
195
735
671
149
148
708
380
646
651
933
931
299
955
838
919
793
953
930
90
946
224
470
735
442
185
247
443
812
907
231
568
483
657
781
354
191
496
999
808
352
629
826
209
350
498
167
762
429
935
531
385
740
867
969
822
173
722
155
55
720
229
677
168
518
219
675
675
887
293
531
BubbleSort - 0 steps
273
739
50
752
818
660
888
967
452
217
543
288
691
292
65
415
311
548
322
614
561
266
603
763
888
840
442
225
917
421
25
838
167
23
570
6
535
306
383
256
571
86
457
436
918
74
944
796
532
88
633
591
68
827
408
972
559
699
149
543
364
999
740
337
686
519
561
996
823
825
68
681
994
280
496
546
360
950
536
72
24
369
740
817
43
517
402
101
971
740
97
638
937
936
676
566
585
956
257
235
InsertionSort - 0 steps
585
153
438
46
890
498
640
798
550
297
478
772
623
986
10
462
556
395
774
656
636
578
111
990
218
860
830
208
808
948
640
609
974
217
618
955
543
918
816
183
502
669
163
4
836
647
384
182
855
120
919
990
584
417
997
826
756
993
919
466
402
261
237
883
456
816
645
434
465
417
345
7
65
54
610
458
87
511
160
840
563
125
335
86
453
404
862
137
834
652
806
789
422
184
962
69
723
120
472
266
ShellSort - 0 steps
128
599
341
630
162
620
693
697
975
90
489
583
206
398
329
498
859
49
520
630
691
793
873
614
438
625
479
834
302
186
672
869
445
537
54
319
701
423
349
386
845
390
762
969
146
148
495
975
638
4
495
937
882
610
80
573
440
395
690
710
321
839
809
629
393
208
307
862
248
10
357
616
296
846
468
557
855
344
217
438
818
998
737
194
481
29
629
614
614
282
268
505
471
103
452
392
343
485
784
679
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