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.

875
828
150
438
857
187
615
46
78
674
504
205
250
858
763
439
430
938
755
88
516
223
116
367
138
472
570
878
327
217
293
959
383
48
161
996
320
382
638
557
152
838
502
702
928
499
725
492
74
747
976
418
954
827
55
349
317
203
86
391
860
92
422
37
469
225
797
19
112
184
749
170
769
587
269
709
588
498
161
852
651
371
661
486
324
612
332
407
458
654
199
930
538
672
624
40
918
426
600
595
BubbleSort - 0 steps
468
396
352
640
602
405
263
118
588
43
942
238
237
419
221
384
654
52
679
30
446
415
133
416
589
311
741
28
112
270
723
447
779
886
26
215
734
96
513
394
888
479
68
794
498
778
994
712
681
279
761
44
442
298
480
803
759
397
854
724
997
806
753
848
935
204
806
64
642
801
820
882
872
473
430
661
823
307
862
243
415
881
390
374
721
600
811
865
680
636
31
718
650
995
254
795
139
779
34
716
InsertionSort - 0 steps
319
384
110
71
715
94
538
192
638
557
913
439
667
516
622
287
952
169
96
822
190
800
949
101
170
946
542
52
892
289
151
938
308
263
942
428
515
666
445
424
507
573
864
731
44
289
270
281
767
825
531
608
32
622
392
405
952
299
6
831
889
368
337
296
826
381
23
896
960
700
386
311
90
864
389
294
534
311
82
960
591
330
666
434
975
62
464
660
687
922
138
76
379
382
703
979
469
808
804
608
ShellSort - 0 steps
854
817
271
154
18
910
125
258
450
506
157
487
132
390
35
459
981
142
752
721
547
191
357
916
457
445
584
298
553
616
607
332
601
918
256
422
494
865
386
727
789
29
316
404
111
496
954
888
212
904
746
667
269
433
183
152
4
525
753
223
424
714
853
719
917
358
575
149
235
820
161
241
161
587
742
878
557
867
530
559
639
35
139
622
247
658
564
543
268
537
577
148
649
975
151
959
955
606
946
697
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