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.

405
630
836
799
968
19
690
344
361
181
82
61
830
763
589
276
346
544
181
744
507
627
403
960
685
402
355
661
206
477
327
75
193
464
677
215
573
225
960
127
633
313
890
446
399
983
190
867
301
216
528
431
784
349
752
333
549
615
778
708
247
236
91
954
161
584
630
317
158
699
272
547
723
605
307
156
486
498
399
825
113
148
762
287
125
216
209
359
178
483
6
488
773
904
723
578
884
482
903
460
BubbleSort - 0 steps
467
722
140
809
122
170
407
845
965
497
448
881
94
836
150
958
892
313
686
806
392
735
435
295
902
740
651
779
475
738
694
702
372
868
493
745
303
727
340
654
500
724
483
581
945
566
439
704
275
402
183
701
809
865
681
14
797
799
66
784
305
314
312
855
209
514
906
56
65
631
979
828
824
406
729
44
796
101
76
379
657
976
85
145
414
749
41
250
318
709
104
18
451
60
828
659
640
254
465
362
InsertionSort - 0 steps
892
29
948
98
117
663
714
902
12
534
731
890
374
979
580
286
319
44
108
373
326
385
683
643
894
31
807
130
449
166
174
71
452
680
236
301
914
202
265
260
272
341
190
344
587
688
324
590
582
83
891
156
426
640
219
118
559
586
286
640
31
559
581
638
384
866
151
706
610
556
340
354
959
888
171
353
635
108
29
251
975
814
116
831
234
665
583
231
166
773
144
717
751
24
669
872
689
126
331
755
ShellSort - 0 steps
17
634
604
295
213
729
620
455
356
836
296
904
516
567
953
259
165
299
704
998
831
424
993
980
656
839
37
36
473
130
360
120
243
558
802
344
358
719
425
881
227
81
976
426
870
819
298
41
473
380
25
88
568
636
704
229
560
126
475
623
248
91
119
667
139
274
734
330
246
113
964
270
506
981
219
913
224
560
109
170
804
180
783
791
633
966
77
703
247
709
681
808
559
876
823
609
474
823
484
710
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