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.

250
374
347
981
873
205
263
723
856
159
588
306
562
766
149
230
713
726
539
231
967
406
110
27
87
313
997
514
993
734
882
995
942
184
789
345
361
390
923
212
378
736
7
379
858
476
404
743
279
941
873
138
951
145
793
805
592
783
471
554
898
88
323
126
851
92
80
926
864
187
51
210
237
476
924
477
769
672
699
988
996
621
51
462
421
142
148
521
355
54
236
439
800
614
790
980
342
76
957
487
BubbleSort - 0 steps
485
982
192
687
611
816
930
368
344
330
709
655
171
715
949
473
858
107
676
714
624
499
212
469
963
281
621
859
851
894
394
267
257
470
807
339
383
183
73
854
652
610
542
34
315
533
511
965
74
735
658
943
292
759
942
547
661
530
733
638
562
521
887
306
557
142
37
744
555
179
389
199
404
303
287
604
448
168
602
886
946
847
456
544
193
868
807
504
498
732
980
228
42
586
185
493
490
880
484
975
InsertionSort - 0 steps
522
353
841
200
251
468
790
960
351
891
542
735
521
458
721
819
989
33
808
776
931
432
807
322
984
267
237
139
320
527
341
330
139
628
881
151
926
141
312
665
656
549
412
423
502
162
826
443
197
582
132
694
839
681
842
904
46
299
796
944
696
710
522
19
696
429
896
249
239
625
860
883
223
402
522
330
564
728
849
283
749
364
561
256
310
361
133
50
857
922
317
426
129
343
898
961
956
593
515
631
ShellSort - 0 steps
403
804
553
636
772
650
129
272
732
840
309
894
321
404
828
679
402
44
722
470
898
984
995
970
8
537
580
618
583
124
787
454
981
254
349
21
838
211
992
848
466
279
400
109
544
895
588
189
808
215
798
741
906
27
436
549
863
528
703
336
345
712
639
656
829
253
834
383
353
158
937
397
609
576
660
691
889
913
2
498
826
912
254
631
157
96
564
501
613
969
213
251
526
581
149
299
413
23
149
467
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