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.

75
341
248
885
465
739
42
606
310
117
457
841
901
90
409
39
406
300
800
897
954
22
397
783
483
492
95
343
589
473
749
58
478
842
808
472
104
853
66
668
578
921
488
229
950
482
733
231
948
585
159
541
707
632
624
942
971
86
609
454
53
786
847
587
771
790
68
383
469
881
353
585
514
589
248
718
895
209
594
498
518
666
243
280
158
105
871
868
792
667
656
909
524
168
331
130
492
191
213
102
BubbleSort - 0 steps
635
968
257
137
711
893
642
295
225
516
657
691
629
756
228
282
238
895
61
483
288
889
474
571
409
627
818
886
537
667
952
590
501
851
59
112
142
180
37
62
318
673
962
472
74
291
502
767
450
427
56
260
247
623
65
232
410
785
660
379
614
681
721
683
832
842
156
553
448
849
887
394
845
328
22
426
945
677
364
42
291
884
756
755
480
279
51
797
782
840
537
341
385
305
524
540
958
313
645
906
InsertionSort - 0 steps
245
109
232
839
663
82
152
105
965
703
944
207
725
384
1
749
178
322
221
777
161
638
743
920
486
836
140
669
822
488
957
668
819
793
830
646
683
866
765
32
251
706
434
29
760
983
463
722
281
244
594
572
690
270
630
623
529
424
244
474
233
135
993
201
997
571
613
293
741
491
252
981
11
321
491
335
370
944
658
128
746
902
705
219
783
650
760
786
317
72
435
285
134
74
566
829
434
543
272
164
ShellSort - 0 steps
112
534
359
265
628
606
14
219
38
525
992
781
798
560
538
38
809
241
626
197
25
908
390
664
425
365
698
864
916
61
406
512
601
774
312
309
995
431
838
353
979
636
14
417
265
64
65
920
907
196
636
30
752
597
49
830
892
680
934
133
98
675
525
699
127
354
455
325
291
100
514
741
616
539
725
653
326
360
751
399
535
632
564
782
156
712
420
554
832
142
245
646
918
836
208
788
58
764
917
71
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