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.

373
814
683
233
971
134
935
807
401
427
873
777
445
156
498
719
665
379
265
678
66
117
504
691
24
437
142
68
841
947
401
703
559
684
50
412
907
165
481
793
31
16
483
976
556
939
813
332
313
201
104
704
292
603
859
406
809
980
862
556
867
853
789
111
184
39
852
152
815
737
808
717
222
8
601
859
685
409
995
418
662
30
971
413
269
882
414
759
406
176
117
995
921
287
415
91
23
473
504
223
BubbleSort - 0 steps
278
635
812
457
670
162
961
514
510
475
662
163
593
76
504
541
913
142
697
796
50
565
195
624
229
848
4
754
699
512
466
902
220
386
735
170
962
952
407
681
977
997
603
933
476
988
363
236
15
219
909
216
73
554
559
909
682
919
719
366
24
839
666
151
561
253
63
220
673
592
543
154
210
554
14
522
508
845
712
215
339
323
325
652
457
686
995
437
757
174
368
284
669
970
688
17
755
128
267
525
InsertionSort - 0 steps
739
335
790
483
955
178
713
796
715
611
763
342
881
226
318
142
864
722
219
368
937
654
85
92
240
395
722
69
345
879
140
913
871
169
933
694
369
144
752
961
864
913
126
460
817
752
493
459
50
611
835
863
139
450
852
24
809
441
546
941
200
818
48
945
512
554
265
229
630
118
403
897
108
572
413
921
96
909
738
593
934
336
386
193
501
469
784
801
93
753
973
262
946
140
427
398
119
130
624
293
ShellSort - 0 steps
241
474
845
56
817
70
750
314
937
533
375
683
262
532
455
476
718
145
430
887
541
138
88
496
334
148
78
10
312
334
163
374
805
153
94
983
542
759
611
390
184
366
399
854
496
473
22
534
644
62
487
556
334
607
66
260
515
555
233
556
475
894
464
630
101
644
358
685
601
740
686
394
899
688
511
92
903
530
120
278
733
338
427
320
689
978
165
894
482
292
763
873
351
274
76
313
477
740
746
794
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