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.

201
525
937
250
441
281
90
340
647
429
820
299
215
632
161
856
48
274
172
33
753
135
553
131
385
36
905
92
488
374
538
313
398
193
168
38
642
833
70
693
669
425
750
921
994
14
171
220
275
410
853
701
502
153
639
634
323
505
937
847
407
802
915
312
435
307
153
302
189
94
829
813
622
126
953
557
201
727
591
707
810
124
130
156
917
654
681
9
595
470
297
208
614
239
110
156
737
58
711
450
BubbleSort - 0 steps
484
245
941
990
674
18
108
817
857
381
174
306
868
334
771
815
292
643
445
784
823
126
879
525
534
163
616
658
894
165
60
56
30
307
114
858
449
699
688
719
464
336
705
981
428
230
532
282
554
371
783
995
107
151
221
602
320
256
664
179
901
834
576
609
333
972
433
338
474
884
145
185
991
656
584
50
410
651
317
984
690
876
694
169
37
177
833
159
663
54
822
462
157
58
429
186
946
999
90
67
InsertionSort - 0 steps
898
280
815
868
308
234
718
524
45
460
644
126
916
103
543
874
548
527
882
233
936
543
148
570
450
977
111
607
59
689
966
949
683
512
484
521
618
740
8
642
654
998
736
807
554
998
352
749
126
195
407
268
668
780
731
323
804
220
59
974
706
922
364
828
774
346
301
656
531
297
3
470
280
359
27
156
271
348
69
520
273
158
731
955
911
743
770
567
435
359
179
185
319
125
272
438
765
780
74
254
ShellSort - 0 steps
405
919
400
155
115
314
136
601
351
629
807
369
447
184
755
300
444
114
982
1000
514
134
834
197
765
818
635
878
449
253
255
377
336
622
201
99
339
285
456
692
117
34
580
454
853
952
986
503
936
675
425
485
805
37
501
476
678
551
260
415
931
993
803
604
947
452
159
243
940
391
936
438
987
645
864
963
532
976
199
942
517
33
448
559
544
800
514
452
624
845
100
647
915
853
469
964
723
974
494
812
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