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.

666
900
502
601
955
239
606
819
589
686
528
759
604
937
193
178
459
174
991
12
968
253
356
310
912
77
188
412
963
132
757
694
254
944
218
426
203
244
759
350
30
619
318
693
658
937
974
395
608
466
718
749
558
321
147
85
139
705
649
65
840
840
524
760
637
260
243
253
578
424
381
809
191
590
553
982
235
285
922
429
422
22
344
873
129
277
885
205
556
853
460
903
237
469
151
745
979
861
534
639
BubbleSort - 0 steps
218
790
332
664
551
261
548
102
4
27
251
378
281
517
763
5
960
12
765
487
318
818
784
894
766
909
590
479
964
944
83
893
944
547
768
343
424
95
573
852
944
637
386
721
471
781
709
398
596
312
863
486
787
417
582
64
25
486
776
30
227
911
50
656
428
241
326
25
964
419
496
705
289
492
364
302
184
555
851
853
476
924
474
627
539
694
256
683
86
372
216
35
981
876
402
230
918
448
300
477
InsertionSort - 0 steps
880
132
65
541
678
575
640
546
634
986
377
619
636
386
221
365
886
806
432
810
607
592
527
273
877
421
51
167
160
473
779
459
505
288
884
532
185
559
889
345
867
731
446
432
855
387
711
41
756
645
878
771
872
461
744
754
493
594
410
3
74
670
951
654
538
861
750
103
183
131
496
824
185
289
510
582
546
795
718
20
59
323
373
293
751
761
548
321
895
744
613
90
233
891
71
905
24
283
45
1000
ShellSort - 0 steps
671
575
962
266
698
163
869
610
941
797
62
967
583
410
482
429
543
777
197
349
853
107
612
181
822
411
285
105
4
852
932
834
123
694
114
558
901
237
268
803
829
734
784
436
634
176
754
622
719
376
319
950
471
867
233
608
529
60
370
574
715
672
575
55
937
920
893
804
881
834
889
265
712
251
185
534
613
308
400
917
340
323
112
829
694
874
154
170
198
55
100
884
550
3
37
123
42
16
269
231
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