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.

683
343
357
299
657
657
88
98
526
560
531
399
823
578
241
730
750
636
423
90
776
950
519
499
485
606
687
605
718
630
887
964
981
871
432
368
88
272
770
874
379
280
331
407
493
984
788
94
402
148
29
465
888
724
888
600
159
683
946
46
219
530
802
781
956
695
186
884
792
233
141
106
426
329
28
487
1
94
595
905
235
840
929
949
806
519
828
304
580
503
95
874
148
797
454
625
90
23
819
675
BubbleSort - 0 steps
769
194
737
460
665
715
497
476
375
138
977
195
186
671
534
219
240
714
256
129
45
274
583
339
196
232
218
112
497
682
345
346
582
674
841
807
833
268
363
259
650
520
240
763
340
994
58
298
579
661
88
807
519
602
632
316
977
142
747
795
953
795
424
386
468
97
803
583
11
121
694
695
280
126
369
833
460
733
340
8
825
828
238
758
941
33
124
15
204
258
355
228
645
389
892
530
343
258
937
550
InsertionSort - 0 steps
267
613
394
215
742
211
661
573
194
940
481
654
134
66
323
400
528
714
313
427
613
927
511
337
702
342
522
931
373
699
774
278
324
404
177
151
132
812
755
382
305
5
919
621
143
535
217
729
693
749
741
840
509
907
287
720
629
219
815
844
133
735
512
230
906
141
365
649
611
337
544
124
594
982
359
722
743
790
202
307
711
242
164
611
646
89
793
229
604
385
689
127
894
343
629
939
334
724
24
665
ShellSort - 0 steps
267
804
619
803
331
767
593
748
421
139
131
567
210
860
557
976
609
314
69
622
585
245
835
245
748
861
410
651
880
703
594
49
752
878
646
414
594
185
696
804
757
215
993
19
440
308
852
972
196
340
126
96
302
644
864
396
77
55
540
537
896
466
626
250
238
223
259
405
85
927
482
895
262
856
451
287
787
267
865
52
939
758
494
20
243
703
937
647
353
575
545
537
807
167
113
214
993
702
13
992
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