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.

303
84
318
479
176
20
934
762
332
426
648
878
686
657
865
389
459
364
299
331
882
534
230
269
365
322
649
594
173
215
484
556
715
137
913
576
66
870
194
187
557
859
433
229
903
927
962
177
83
716
905
889
529
12
613
640
335
211
123
697
336
578
327
497
461
849
58
928
208
475
77
779
174
439
858
828
501
917
284
940
440
69
670
468
740
546
580
712
619
388
354
550
982
31
671
103
941
930
979
639
BubbleSort - 0 steps
610
838
19
85
392
725
856
872
541
304
638
589
457
37
793
407
936
924
836
789
259
57
387
145
302
576
610
926
373
703
12
790
117
115
85
691
169
27
725
333
236
968
607
95
746
464
665
399
460
906
157
873
287
883
979
878
519
341
792
317
515
91
1000
440
330
895
792
156
896
581
234
906
277
452
723
326
721
834
854
816
181
897
783
885
503
900
545
909
719
375
563
684
151
423
354
697
741
382
672
651
InsertionSort - 0 steps
162
99
753
213
442
656
199
814
734
104
125
472
493
135
625
390
461
967
269
462
437
663
241
206
392
305
872
263
238
646
264
452
693
869
79
999
28
716
129
355
731
630
975
851
349
827
594
214
483
874
744
924
872
682
297
617
873
692
707
775
230
167
573
303
630
936
357
472
725
761
945
278
39
554
703
249
205
690
351
813
81
836
577
357
16
878
521
153
540
557
547
256
342
884
105
601
915
487
649
835
ShellSort - 0 steps
88
518
985
589
963
821
756
594
355
665
376
186
877
260
112
863
749
158
453
603
431
591
542
514
946
448
12
342
882
241
130
653
602
866
537
353
328
98
519
620
991
469
132
68
805
491
416
961
91
373
291
14
172
347
44
527
401
446
463
329
697
164
353
405
602
56
644
155
487
967
709
751
873
812
937
920
87
937
106
366
254
653
460
646
224
252
666
604
313
725
255
174
109
64
172
505
994
842
395
378
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