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.

575
494
992
441
824
646
872
526
965
126
792
838
720
979
278
255
74
604
856
197
734
421
890
118
668
350
853
274
176
940
326
475
395
84
776
947
901
782
665
787
547
499
600
64
176
336
699
866
854
306
871
523
536
358
287
210
371
100
832
120
612
324
893
814
73
436
355
828
691
592
144
167
990
207
339
516
300
246
207
929
4
820
923
354
728
665
149
5
238
473
425
414
849
149
242
645
481
608
223
227
BubbleSort - 0 steps
782
546
908
414
433
467
534
384
545
622
308
875
381
734
626
26
289
561
726
700
941
565
213
403
567
319
533
258
38
338
733
758
38
480
682
276
271
252
828
241
258
720
520
263
164
160
448
10
66
401
874
388
188
911
668
676
887
439
672
848
161
873
898
332
449
647
687
740
369
108
513
517
418
687
105
84
511
973
746
781
793
547
757
925
334
44
608
826
858
115
750
595
377
524
60
48
462
649
302
221
InsertionSort - 0 steps
990
551
883
124
857
489
766
238
746
25
866
588
489
546
311
634
384
814
865
632
464
797
968
627
332
202
471
208
599
777
138
231
292
100
643
798
600
387
524
590
902
968
601
644
878
310
219
577
255
641
29
952
743
394
755
328
113
288
705
219
387
128
830
401
694
668
785
130
17
287
138
691
269
805
907
571
747
630
179
644
783
416
926
217
443
111
60
912
817
769
167
389
746
239
877
794
308
518
740
551
ShellSort - 0 steps
774
797
773
468
123
387
894
721
920
200
166
946
720
980
351
767
185
842
313
48
962
684
918
588
384
472
343
20
194
163
18
690
943
550
667
996
593
886
749
310
435
872
403
335
751
369
145
496
813
919
681
342
127
748
818
237
568
907
996
338
990
483
666
640
238
113
685
545
823
694
602
683
31
557
480
725
450
806
30
845
985
765
31
321
646
7
284
480
165
580
436
542
655
521
261
905
869
149
972
241
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