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.

787
556
793
466
596
953
42
72
297
576
318
275
113
69
680
685
562
346
684
938
989
473
936
262
909
116
490
697
84
178
107
224
750
829
404
133
867
217
659
153
348
949
360
505
417
741
539
193
255
395
633
380
574
644
116
642
122
982
397
486
167
36
489
235
8
777
693
778
952
12
96
958
726
533
354
331
402
698
724
481
586
474
429
63
821
296
840
929
605
5
126
400
940
629
177
338
750
293
100
414
BubbleSort - 0 steps
907
434
744
422
837
916
840
81
550
722
291
360
202
960
965
974
946
656
55
901
499
998
835
239
97
982
794
525
430
823
706
282
189
589
764
143
880
802
998
491
919
394
666
729
476
90
633
275
387
952
246
642
846
67
660
50
542
291
489
407
671
955
29
160
541
652
85
906
199
464
568
363
634
34
386
292
792
957
472
359
647
273
59
279
381
913
424
379
279
683
475
779
532
273
189
836
259
502
219
880
InsertionSort - 0 steps
840
879
983
951
548
732
850
628
764
960
171
957
652
88
543
639
843
86
68
15
563
352
274
776
346
838
727
335
790
688
151
494
113
641
332
714
965
661
641
887
238
906
26
685
573
178
765
682
402
10
424
193
53
688
246
849
455
394
404
660
590
732
991
607
196
356
105
233
755
907
945
904
503
621
88
266
29
9
672
403
991
657
612
667
436
730
909
987
290
75
959
62
395
109
856
27
631
819
880
885
ShellSort - 0 steps
367
507
361
765
372
457
888
955
195
798
75
70
439
933
996
751
585
415
181
189
650
385
529
805
732
939
180
172
695
992
187
814
12
496
514
953
339
95
358
266
561
608
577
207
407
438
164
110
1
407
119
721
173
829
662
547
534
288
604
743
313
807
469
961
698
962
121
354
428
167
285
560
623
891
937
586
42
153
844
159
650
925
724
368
374
596
596
416
951
91
224
486
862
554
757
401
616
509
91
891
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