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.

212
349
443
839
414
558
841
132
694
125
423
283
959
930
944
794
377
564
406
966
571
991
449
310
61
663
400
974
245
101
81
567
766
600
876
537
151
232
928
528
129
885
203
915
356
295
868
140
612
292
736
314
349
2
39
46
830
277
253
851
729
394
583
855
508
115
922
104
957
535
497
23
810
375
449
949
909
664
343
176
828
683
976
529
916
894
787
756
583
678
82
832
734
181
758
169
564
662
512
134
BubbleSort - 0 steps
958
541
476
650
847
77
682
212
684
50
649
207
351
256
949
223
133
148
528
639
465
703
217
118
234
199
740
146
721
286
99
663
435
251
846
461
669
220
214
859
140
459
594
631
857
974
81
8
137
435
198
124
176
48
754
306
218
264
266
883
391
475
617
307
943
310
365
561
351
144
752
425
135
748
640
5
806
534
45
629
750
216
887
619
647
580
327
615
221
966
783
343
616
364
163
985
820
589
540
961
InsertionSort - 0 steps
320
220
329
105
364
295
48
129
230
110
598
282
551
572
441
552
398
657
102
313
57
657
163
717
544
580
210
818
914
173
834
659
298
227
947
675
222
122
998
320
880
69
448
978
721
473
818
691
780
944
103
995
278
863
221
318
261
780
485
344
148
13
271
697
484
373
216
611
600
135
490
396
47
172
44
471
638
339
22
932
466
390
897
618
4
517
905
2
340
481
365
28
539
318
705
735
195
533
21
781
ShellSort - 0 steps
460
665
985
496
41
840
686
181
716
925
813
231
872
265
103
247
499
794
65
766
386
246
37
876
744
890
281
763
680
807
436
778
39
109
136
50
328
372
263
239
500
711
101
818
345
583
782
964
315
987
870
440
99
173
754
862
275
356
905
86
905
431
188
520
59
908
311
189
497
76
102
74
321
860
676
481
174
942
134
159
837
40
149
989
398
3
896
674
721
985
613
489
377
777
614
576
974
63
879
674
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