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.

486
267
564
120
275
429
153
632
194
783
14
925
467
158
919
662
362
934
576
515
624
715
422
337
380
466
577
201
721
542
998
657
401
566
370
138
185
313
421
207
10
787
615
658
971
69
887
590
403
665
819
134
649
941
489
400
226
729
707
447
837
770
588
200
71
708
280
110
933
579
235
626
720
280
615
897
744
223
461
842
484
157
91
763
761
4
243
938
868
480
887
61
106
433
26
625
404
733
832
550
BubbleSort - 0 steps
984
955
793
520
169
285
110
72
988
163
969
361
891
448
917
535
128
301
27
672
6
13
864
477
611
698
925
161
262
12
919
357
104
905
510
58
186
965
401
745
855
363
384
331
247
982
97
722
503
371
419
677
807
518
209
922
755
432
660
509
221
77
105
49
319
451
481
424
275
537
546
402
80
973
278
216
418
287
509
722
207
431
902
693
228
728
354
758
253
488
30
387
79
749
684
132
791
863
205
218
InsertionSort - 0 steps
236
764
264
599
922
837
939
126
609
607
918
537
425
299
596
544
757
213
993
217
338
411
207
564
730
548
198
451
815
10
637
332
793
496
320
540
82
453
208
481
269
548
155
425
992
934
479
614
242
420
556
249
36
879
214
713
576
803
752
89
255
286
632
512
337
451
402
98
122
866
723
18
828
772
673
279
66
619
633
41
860
237
863
471
987
703
536
56
765
624
201
522
673
203
358
163
419
277
380
796
ShellSort - 0 steps
130
415
813
376
232
502
407
772
416
275
912
235
950
911
694
961
229
54
648
927
309
900
103
164
694
248
15
64
896
102
138
764
828
760
630
382
666
858
32
616
813
549
553
115
368
644
823
662
989
612
819
647
216
394
39
694
575
368
382
461
396
112
206
715
874
164
96
714
227
566
15
164
410
227
710
832
486
815
593
953
691
579
231
314
260
36
253
659
856
897
588
825
178
60
711
145
291
237
39
188
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