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.

930
724
560
973
831
747
772
177
632
899
341
108
115
115
712
609
350
305
643
105
43
600
570
587
127
386
830
783
846
724
234
674
10
644
932
550
978
899
716
401
13
821
280
59
196
961
69
874
52
395
563
840
73
433
677
180
748
23
178
762
203
921
3
354
921
190
908
274
56
71
454
477
622
538
667
656
820
528
290
523
25
596
471
488
617
338
793
727
616
818
626
21
36
638
684
404
254
344
459
218
BubbleSort - 0 steps
772
312
373
756
661
16
482
66
312
799
536
409
908
35
540
126
291
704
295
243
955
662
974
817
978
701
55
787
387
298
466
779
523
324
496
453
279
770
850
294
490
907
224
478
927
955
973
960
216
351
725
57
354
170
346
297
976
952
165
920
54
212
941
999
441
624
890
641
209
669
983
954
584
402
780
232
806
47
619
477
235
964
134
430
411
180
274
366
989
288
156
923
994
534
644
859
489
64
909
622
InsertionSort - 0 steps
157
640
411
456
84
228
446
324
683
266
692
721
769
445
128
314
237
729
673
736
656
116
165
226
180
386
513
240
336
883
278
695
339
226
788
564
82
168
499
811
222
155
572
801
111
15
840
650
294
757
822
409
866
842
286
472
685
844
85
817
771
103
841
45
693
600
33
112
398
413
274
235
643
632
936
947
976
708
371
808
560
921
252
273
774
127
317
222
758
295
237
875
807
176
558
99
170
506
910
463
ShellSort - 0 steps
74
778
305
783
110
346
209
62
469
884
258
812
137
955
395
521
326
392
496
713
881
931
701
687
704
781
931
116
92
13
30
72
598
289
840
76
305
507
11
838
724
752
515
450
244
827
429
415
827
615
979
938
394
406
105
638
356
622
438
747
999
565
633
509
101
541
185
75
181
651
489
857
980
777
260
918
957
558
154
497
181
334
203
902
64
744
287
20
241
149
559
663
581
305
759
335
502
486
971
725
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