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.

645
833
534
695
300
178
992
941
786
64
31
7
758
616
659
953
931
717
217
527
596
818
64
466
821
569
734
694
389
251
415
769
269
134
398
609
29
148
397
769
791
510
447
450
993
667
15
861
307
808
641
908
923
571
214
52
674
963
303
750
719
575
639
525
51
250
829
860
293
691
233
520
707
29
122
471
48
480
493
373
341
958
103
40
866
124
629
813
524
351
944
813
741
327
726
82
83
66
804
207
BubbleSort - 0 steps
260
479
425
42
751
71
870
73
904
451
968
996
251
507
235
136
942
622
536
669
142
993
755
548
795
952
192
681
465
602
677
115
14
839
703
34
358
892
674
121
784
864
606
580
269
14
974
514
725
703
387
112
752
442
134
954
503
9
205
50
186
330
707
880
978
58
838
41
769
764
302
169
67
379
767
393
95
774
394
483
858
611
250
379
521
7
963
816
649
900
320
997
942
709
739
243
133
999
910
311
InsertionSort - 0 steps
473
817
307
487
185
604
711
407
10
393
162
762
799
866
455
596
762
92
15
118
107
556
710
557
661
204
139
673
274
102
957
969
276
285
583
693
619
582
327
236
398
119
642
235
236
397
42
965
688
252
289
506
860
636
153
666
460
680
177
450
206
677
453
86
974
744
834
593
86
477
871
384
533
552
204
573
584
556
371
825
743
20
111
362
347
960
767
841
1000
535
54
195
65
104
207
847
403
154
955
831
ShellSort - 0 steps
551
594
395
76
560
124
244
577
368
241
15
577
187
275
981
151
84
458
560
262
880
980
329
476
813
597
352
137
413
469
824
796
604
777
584
83
828
231
567
293
342
94
305
210
773
501
195
986
39
448
435
686
951
442
306
25
295
54
721
357
555
804
141
565
771
657
535
902
957
82
392
822
530
341
151
380
153
741
524
617
98
859
974
548
827
69
309
551
3
337
900
256
225
134
120
642
263
788
645
9
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