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.

338
172
326
565
911
329
566
274
345
949
236
704
622
996
914
824
214
46
548
529
128
780
46
700
344
86
863
162
677
342
851
99
867
155
436
792
125
35
106
745
685
71
738
340
787
258
301
764
308
941
350
490
544
612
575
170
711
416
97
463
339
488
187
128
385
567
496
685
159
560
649
474
253
445
259
607
374
103
284
708
791
973
185
683
723
995
49
891
489
735
78
448
193
555
97
306
23
103
389
881
BubbleSort - 0 steps
342
554
96
850
415
262
608
523
763
430
407
969
248
624
740
38
885
521
898
711
484
184
803
604
246
457
482
847
604
961
183
871
730
539
672
283
808
672
365
461
536
953
132
592
828
424
733
203
527
229
628
757
166
699
427
218
489
872
678
120
47
547
15
407
312
110
697
697
344
573
586
42
942
996
948
578
202
286
374
433
911
169
82
116
522
849
778
699
344
309
741
378
760
855
616
108
113
60
959
980
InsertionSort - 0 steps
592
399
982
987
17
727
347
546
313
136
792
685
752
25
321
611
978
19
14
710
868
808
620
971
113
941
837
902
239
122
918
595
148
621
380
164
1
783
853
86
77
602
662
105
41
76
254
499
702
861
457
931
280
494
244
327
126
795
766
71
854
200
445
702
239
913
729
368
642
955
543
114
1
266
319
44
945
504
486
694
950
496
571
451
44
555
122
883
822
619
189
591
117
633
593
872
171
79
877
766
ShellSort - 0 steps
2
358
504
283
219
56
332
813
171
2
531
419
985
185
317
634
340
133
527
922
181
465
327
111
316
714
89
784
597
733
405
842
868
204
346
77
281
397
822
361
667
272
360
783
740
889
451
561
959
958
226
641
278
257
152
444
25
736
393
53
67
635
347
951
172
318
18
58
532
785
482
64
496
92
408
358
900
223
563
296
233
222
778
52
304
97
305
116
390
573
648
443
760
330
564
55
388
581
713
561
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