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.

631
778
781
567
759
590
246
526
397
745
137
853
475
444
285
351
198
950
940
651
274
655
168
518
619
510
540
498
486
979
693
822
215
469
44
118
723
413
524
857
36
772
825
142
991
604
1000
623
912
946
986
408
682
606
455
481
805
688
109
676
638
868
886
470
703
955
24
397
377
85
958
695
959
164
447
507
555
282
855
77
247
14
794
507
367
638
528
113
51
989
332
305
942
189
314
10
771
372
730
249
BubbleSort - 0 steps
181
467
175
948
768
989
201
530
986
239
638
637
466
163
109
929
228
532
831
724
562
434
86
166
181
442
319
910
595
808
869
579
83
207
511
736
367
723
122
602
498
317
823
402
5
549
542
658
710
874
593
219
162
901
227
211
794
623
557
90
963
445
409
867
945
787
184
41
202
559
504
204
173
201
934
688
21
635
260
710
294
585
336
363
65
974
213
527
535
825
416
245
344
248
120
138
926
358
217
254
InsertionSort - 0 steps
610
65
759
308
628
950
897
41
919
837
463
939
44
251
877
771
307
360
895
806
804
112
298
448
52
396
701
153
142
640
611
355
932
997
628
5
572
983
340
138
736
654
650
254
58
634
547
986
790
277
200
554
655
136
921
838
368
824
718
211
308
939
816
796
152
713
636
883
158
789
32
251
86
933
355
874
104
723
635
987
118
228
131
243
666
780
285
535
815
84
489
436
118
544
500
454
609
655
151
715
ShellSort - 0 steps
888
321
480
929
2
121
605
968
743
361
977
542
131
756
549
38
984
653
466
292
59
94
319
449
278
148
942
940
270
529
125
450
119
630
402
241
283
390
686
967
836
922
278
403
14
882
616
964
604
381
270
241
492
636
535
972
681
470
815
649
571
928
804
611
766
466
738
671
480
920
382
440
278
605
19
604
426
331
928
869
195
329
639
914
912
954
80
223
215
912
495
650
801
371
874
352
748
193
916
12
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