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.

298
180
742
888
314
256
720
54
468
631
323
206
65
86
538
657
819
612
868
791
420
687
739
129
628
468
325
380
77
102
254
584
818
983
150
970
7
715
55
519
405
224
405
916
917
455
387
881
159
556
41
10
732
277
85
86
527
625
205
843
509
139
456
676
224
694
919
753
324
124
153
585
360
802
755
541
664
779
947
518
843
337
769
212
186
911
568
773
591
308
237
299
838
861
975
835
93
603
915
251
BubbleSort - 0 steps
508
922
63
368
90
716
123
716
88
132
433
82
812
139
144
543
351
853
444
335
669
463
829
466
970
924
220
723
342
984
712
254
879
426
170
542
559
841
934
390
421
715
694
867
691
141
616
764
230
702
470
871
429
955
560
158
284
523
947
110
786
129
993
167
625
673
634
767
25
765
650
749
675
28
487
541
728
7
284
949
611
53
316
174
809
543
192
126
8
763
895
703
899
242
423
273
82
564
898
593
InsertionSort - 0 steps
489
44
538
644
495
387
165
281
825
130
215
731
790
146
249
129
421
686
465
588
793
328
592
458
373
104
457
847
95
200
723
381
942
369
985
905
532
797
443
453
968
873
357
999
16
483
404
165
867
14
428
152
77
444
168
289
194
497
963
502
333
996
36
886
535
396
128
991
511
167
699
549
543
830
565
862
554
378
61
981
911
699
794
253
308
578
8
113
356
974
850
943
280
353
13
531
147
862
212
803
ShellSort - 0 steps
403
14
519
532
110
486
887
744
523
629
475
718
134
762
332
240
917
58
54
804
979
777
213
482
652
147
395
928
701
594
98
897
81
37
653
621
630
44
97
275
433
102
994
71
545
446
583
371
473
395
923
308
986
605
187
693
634
251
201
660
60
760
143
203
472
166
527
478
344
825
396
745
533
194
335
902
849
29
603
289
689
127
151
28
24
105
401
791
38
955
556
114
331
535
25
191
565
211
754
585
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