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.

618
70
68
956
663
678
459
653
721
559
456
365
22
577
797
866
205
991
456
640
483
780
386
604
45
319
113
819
487
726
512
861
687
650
894
69
181
760
152
264
194
489
335
981
990
940
839
990
632
609
36
656
708
973
336
800
903
561
700
642
486
847
732
937
276
644
480
586
314
50
845
826
862
20
399
931
271
900
206
240
101
634
841
25
529
335
910
681
130
761
965
925
828
776
161
477
369
614
825
413
BubbleSort - 0 steps
646
450
54
850
367
285
714
7
182
358
357
401
677
176
479
789
927
53
194
579
198
614
608
150
462
705
760
739
72
40
130
544
254
45
860
76
120
252
172
123
75
511
710
748
465
976
620
835
541
90
97
930
331
435
150
246
520
870
710
995
186
954
923
910
306
756
652
393
490
481
403
352
239
482
290
685
954
294
574
441
741
837
742
284
671
334
983
917
795
160
455
390
331
105
293
328
140
234
204
484
InsertionSort - 0 steps
578
371
353
865
59
792
627
945
805
749
627
956
991
926
306
317
155
219
935
984
10
672
9
614
158
109
251
254
723
680
226
358
804
545
915
492
372
191
622
368
728
768
420
70
13
130
284
516
474
745
237
898
663
407
788
430
780
821
796
482
245
600
965
139
521
507
582
296
292
226
359
96
976
489
256
816
39
544
295
405
940
890
126
230
295
181
811
623
625
579
234
725
261
227
615
223
680
682
963
913
ShellSort - 0 steps
42
104
291
518
92
524
381
234
711
444
207
421
90
103
857
942
571
790
600
307
644
431
862
74
861
306
785
161
779
246
867
708
905
614
57
834
77
101
579
107
978
439
930
963
862
430
315
355
730
248
662
72
742
211
682
188
278
603
545
391
296
574
767
734
15
197
78
373
728
219
383
435
288
227
506
691
585
986
290
159
644
649
539
836
151
21
754
862
508
396
719
452
25
662
333
554
813
576
745
977
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