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.

8
712
826
664
322
91
111
281
793
489
661
876
577
118
183
811
722
760
307
765
407
874
733
869
933
143
631
353
660
428
117
116
154
258
610
901
870
752
195
421
135
579
912
311
246
99
407
915
167
717
766
701
899
38
796
591
296
427
853
188
441
611
216
2
381
810
104
939
645
930
262
519
455
528
524
625
330
215
333
142
789
451
487
797
191
944
706
433
164
178
106
473
939
112
441
484
90
148
720
238
BubbleSort - 0 steps
26
676
877
447
585
73
921
816
184
745
875
957
350
532
742
845
538
709
1
845
512
187
449
99
760
361
832
107
691
312
943
598
203
504
761
302
70
397
825
442
693
113
990
587
593
168
162
469
884
724
13
955
606
376
51
131
693
590
185
574
746
159
105
536
973
51
849
756
170
6
809
538
868
384
644
960
530
764
88
414
58
624
627
734
289
235
622
672
595
642
562
531
560
846
317
707
864
92
592
725
InsertionSort - 0 steps
966
532
173
165
838
162
927
932
40
389
595
783
349
777
510
714
237
818
970
556
907
253
943
486
982
558
876
465
421
675
657
872
532
391
725
432
830
188
478
796
256
323
618
752
371
499
402
473
187
860
898
926
825
897
687
553
955
596
620
616
526
244
647
734
227
59
292
800
869
598
12
448
858
285
278
657
416
931
557
75
455
865
511
595
686
808
27
316
462
642
568
577
257
335
349
50
213
644
385
665
ShellSort - 0 steps
166
85
637
837
566
346
14
536
348
332
810
621
688
449
679
413
384
691
469
520
862
364
86
779
225
582
373
430
147
513
997
984
957
628
225
450
736
573
355
445
248
585
935
988
391
69
308
133
932
89
374
944
354
33
843
324
144
953
171
784
5
594
203
198
793
865
86
687
752
150
632
444
564
935
722
198
751
851
393
233
867
578
42
720
720
757
515
838
859
881
590
830
424
320
458
326
306
398
761
339
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