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.

730
860
759
391
531
435
712
399
221
749
549
941
589
716
60
336
310
472
218
297
511
336
486
608
387
240
134
649
232
457
29
771
483
229
131
830
27
464
864
801
13
866
757
160
495
822
835
43
792
14
584
858
572
511
523
430
76
781
282
38
928
293
211
910
630
151
718
866
41
246
144
978
462
773
741
163
777
207
412
179
669
688
8
587
860
841
652
34
106
304
753
277
156
555
739
727
698
632
75
288
BubbleSort - 0 steps
727
899
38
181
606
148
867
579
339
239
446
309
750
823
428
943
17
43
435
889
376
203
179
823
151
429
329
557
129
977
520
75
273
80
12
30
916
560
339
905
975
712
361
73
576
763
597
160
294
76
389
848
2
900
71
788
968
539
981
379
834
656
493
65
243
22
115
840
793
986
553
284
81
760
939
98
152
910
826
566
552
394
662
995
434
672
159
841
872
161
854
557
296
336
493
733
173
702
586
694
InsertionSort - 0 steps
745
118
846
860
855
755
106
372
733
107
722
334
153
978
887
662
746
330
630
188
816
80
534
673
851
529
71
457
617
768
747
218
375
887
283
729
279
791
803
981
996
188
148
573
85
507
189
471
367
923
8
299
817
37
530
676
507
723
661
910
937
371
680
696
861
630
544
856
507
46
966
186
331
159
233
635
202
722
25
386
860
540
354
360
597
141
485
643
807
261
86
141
460
934
580
101
362
205
594
577
ShellSort - 0 steps
501
272
788
861
829
150
600
313
721
353
618
484
959
436
567
793
360
952
264
403
727
374
214
784
281
622
428
523
675
538
144
772
475
806
208
337
806
4
771
952
764
368
106
618
29
603
395
610
419
476
805
73
149
187
392
922
655
624
239
927
746
774
278
333
773
528
560
658
835
356
289
717
462
611
44
983
878
666
323
661
350
309
58
265
709
968
526
205
964
956
141
739
938
479
231
546
795
581
59
30
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