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.

508
473
844
542
534
132
1
501
191
94
318
216
91
276
989
678
17
467
482
618
239
118
697
885
936
159
698
744
325
747
502
629
927
214
685
363
720
539
214
814
526
355
212
514
511
86
675
366
748
788
28
104
125
524
285
630
113
995
83
902
545
840
403
687
179
851
925
500
885
614
666
303
828
964
469
284
408
905
10
1000
350
19
606
671
825
310
322
203
815
614
736
278
626
672
571
117
430
606
536
893
BubbleSort - 0 steps
250
418
193
950
79
539
394
217
702
752
787
28
895
900
238
320
882
925
166
887
363
585
301
990
659
699
416
220
665
186
191
897
790
522
909
659
414
500
314
690
406
157
517
664
871
671
658
97
508
102
986
833
558
959
286
169
733
172
866
246
459
20
814
803
882
891
56
75
731
299
283
346
18
547
600
598
156
980
601
411
709
846
314
967
507
631
741
65
432
138
851
937
557
349
250
426
877
63
939
189
InsertionSort - 0 steps
383
747
667
936
597
193
141
353
873
774
778
243
552
321
107
990
547
442
496
709
205
178
34
70
945
311
235
80
827
875
626
263
51
484
230
581
622
898
551
21
187
336
929
885
487
488
574
92
639
538
588
24
213
234
808
983
367
839
633
634
888
397
501
515
244
404
365
405
784
828
187
351
838
790
589
79
849
967
743
906
545
258
909
85
316
825
294
263
886
720
88
908
199
957
876
526
405
796
628
890
ShellSort - 0 steps
572
468
382
682
231
926
144
326
275
181
218
610
620
34
30
997
856
430
82
607
662
916
325
114
123
745
910
579
649
669
504
914
12
315
927
700
470
691
26
933
613
740
61
280
835
862
275
765
578
124
34
193
411
617
645
725
480
175
773
345
161
64
986
475
696
940
630
548
512
582
346
652
361
325
495
786
994
168
118
42
860
828
689
461
82
818
990
768
154
281
84
713
405
309
650
380
173
781
459
84
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