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.

720
216
258
490
239
97
510
305
848
319
778
369
482
985
89
627
625
627
288
25
687
383
309
122
931
270
445
610
612
756
795
783
877
83
363
55
129
877
64
51
455
238
960
996
898
522
167
276
393
641
480
405
128
834
358
975
361
932
561
441
810
207
767
501
138
9
122
599
275
502
691
788
239
59
443
106
669
820
854
693
255
261
395
67
677
755
985
138
71
191
931
66
896
324
747
665
868
109
304
813
BubbleSort - 0 steps
5
375
851
654
999
672
445
213
694
806
495
797
398
420
234
866
576
187
6
249
976
30
889
938
126
652
358
69
372
725
937
659
480
33
636
638
600
857
510
474
349
670
866
757
137
778
108
581
267
821
510
48
346
919
959
50
913
581
257
719
338
267
970
928
974
272
649
371
821
759
483
339
110
791
467
579
651
644
182
258
827
882
853
902
792
772
321
27
661
268
62
884
57
161
436
309
592
129
505
807
InsertionSort - 0 steps
889
1
672
209
518
393
305
535
420
547
966
729
631
141
676
856
295
892
206
854
405
575
48
578
25
748
934
271
927
140
749
520
465
958
317
149
798
995
796
686
292
819
522
501
689
404
37
432
2
707
651
75
747
490
721
406
372
474
889
193
942
22
755
806
59
83
576
547
231
369
941
280
194
73
406
550
110
24
533
751
111
355
849
780
873
784
56
113
854
57
121
976
591
908
159
937
559
144
550
387
ShellSort - 0 steps
106
536
119
151
618
144
913
170
886
259
492
500
239
889
461
711
318
265
5
149
873
226
330
641
46
156
24
816
691
16
803
2
232
910
367
684
450
46
519
21
12
932
209
263
405
391
132
119
402
81
544
108
569
919
20
418
684
823
630
617
48
377
798
89
446
928
208
653
31
865
709
90
475
701
21
736
16
920
115
85
471
691
727
19
334
180
12
307
8
540
433
467
95
313
772
230
734
777
831
599
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