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.

351
616
515
55
431
815
761
498
947
757
53
250
268
651
677
675
256
850
194
507
774
869
417
494
651
346
892
553
199
66
355
971
357
784
774
945
823
264
804
929
808
382
164
406
28
300
984
693
124
656
937
300
90
571
327
92
245
117
47
865
881
21
191
131
149
526
587
44
635
426
694
439
186
786
687
693
462
33
8
669
359
489
963
657
317
756
409
223
985
862
209
933
120
947
420
315
185
976
889
671
BubbleSort - 0 steps
917
282
745
59
343
777
448
725
474
529
709
438
243
724
744
74
937
140
379
561
981
497
549
523
651
666
274
646
947
304
722
356
411
285
134
39
721
197
701
728
780
887
230
740
852
106
731
23
775
859
939
454
382
632
151
725
350
620
353
330
709
26
102
643
302
97
744
809
225
352
521
349
439
274
812
82
517
748
610
372
473
607
323
756
882
369
207
597
866
616
612
663
487
50
249
446
404
831
464
725
InsertionSort - 0 steps
237
68
95
366
788
407
161
38
681
549
125
859
673
333
120
695
781
491
968
714
907
568
452
806
108
986
550
948
546
972
356
300
216
945
380
137
236
223
24
221
137
536
606
766
884
338
422
324
709
528
319
50
265
731
570
437
809
133
412
438
435
217
7
980
93
792
353
645
230
856
592
688
136
532
311
622
616
594
364
965
172
728
362
103
762
113
906
286
502
387
547
831
669
154
836
357
602
326
688
627
ShellSort - 0 steps
494
488
501
14
850
576
474
854
33
900
454
173
798
805
462
630
684
948
197
343
332
328
831
179
592
717
81
45
664
21
6
737
365
677
61
861
323
95
646
929
97
28
796
149
301
167
140
886
830
543
681
493
886
289
81
640
359
150
428
948
150
800
31
747
540
779
109
769
998
269
811
799
89
210
107
128
156
875
863
74
849
284
369
309
579
764
602
894
722
378
497
705
914
653
41
407
250
153
109
864
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