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.

878
242
107
802
884
818
299
502
264
49
623
185
241
616
498
611
561
648
300
393
666
688
6
421
943
252
337
10
452
813
375
180
657
52
494
330
616
962
647
626
709
986
161
639
796
850
628
411
419
789
761
641
483
499
636
522
506
19
390
245
958
305
709
792
546
652
592
518
79
792
354
48
182
907
900
769
813
107
12
964
757
552
760
970
815
187
739
652
157
956
175
512
22
935
722
716
342
755
79
536
BubbleSort - 0 steps
259
402
554
37
213
558
561
409
199
93
727
219
978
212
509
984
850
533
480
639
619
595
816
407
997
293
172
713
381
826
930
10
388
492
642
93
56
260
88
232
322
333
156
63
256
400
119
365
466
568
715
902
361
595
653
417
497
163
325
827
22
408
424
654
387
505
42
124
553
478
740
964
809
787
907
343
826
895
390
112
877
257
144
957
37
251
753
590
795
675
855
962
886
457
866
470
335
148
117
783
InsertionSort - 0 steps
618
963
562
80
747
760
232
212
626
525
834
832
841
531
628
67
303
270
646
189
700
257
499
345
273
496
529
421
866
509
515
406
400
334
184
247
372
821
364
7
328
922
390
682
784
942
720
385
827
77
1
210
505
566
575
160
166
781
912
484
80
135
452
650
616
985
876
320
980
35
901
854
69
883
513
754
196
444
227
720
265
665
151
164
27
547
35
707
513
962
816
19
689
539
991
79
135
443
293
864
ShellSort - 0 steps
616
144
136
68
322
987
660
486
39
258
44
256
13
918
598
753
335
684
389
465
836
278
565
427
941
371
897
633
631
929
722
323
343
765
401
131
58
41
946
177
283
207
353
793
586
517
345
3
84
290
530
662
735
259
768
839
419
818
526
487
299
678
143
58
412
954
944
717
140
144
245
206
379
891
873
157
636
701
234
605
555
265
684
434
177
466
229
959
314
403
119
94
165
243
941
53
303
115
913
233
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