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.

935
862
875
1
45
39
134
363
3
935
6
677
618
398
581
653
185
146
300
614
873
250
326
128
49
215
565
810
828
86
157
521
227
929
430
969
890
227
982
112
724
658
53
79
540
253
393
349
278
614
395
870
167
548
970
184
136
622
86
318
833
979
227
539
332
890
332
495
168
640
101
888
967
834
304
810
71
286
496
923
377
93
716
35
350
475
20
910
115
500
731
772
156
801
854
746
780
520
633
818
BubbleSort - 0 steps
220
305
211
181
951
388
685
248
246
34
83
185
946
410
530
732
984
719
906
112
904
959
86
209
901
118
483
297
397
722
920
788
823
984
399
185
880
288
240
102
172
730
417
707
66
704
433
490
182
611
212
694
565
707
104
101
116
597
109
261
972
435
161
308
444
890
237
929
939
894
402
707
72
693
395
906
108
72
31
526
5
348
342
831
207
239
347
615
82
424
81
12
363
770
615
501
21
931
544
427
InsertionSort - 0 steps
78
679
179
588
889
126
500
10
327
441
758
32
163
469
108
72
599
367
466
425
244
14
204
3
476
454
568
999
206
883
681
889
15
570
137
322
947
2
114
173
757
982
312
229
325
414
533
255
491
825
73
826
654
281
174
721
111
517
955
16
988
911
451
648
601
410
507
711
892
662
214
568
270
675
886
678
62
74
234
54
491
198
886
557
238
51
250
136
899
292
931
824
852
407
481
356
731
767
311
994
ShellSort - 0 steps
977
716
995
271
122
522
885
856
800
59
402
59
130
661
864
20
958
142
17
226
542
390
823
770
813
436
697
615
270
968
3
846
644
462
432
48
835
838
212
438
614
422
203
771
270
100
947
198
721
224
265
541
222
208
301
928
931
581
194
550
972
156
913
605
609
240
500
542
578
526
967
857
531
172
258
27
903
229
766
399
271
529
907
36
984
963
629
814
196
474
724
121
786
259
269
58
201
426
323
503
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