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.

982
885
448
720
281
242
339
925
733
579
213
360
415
956
48
478
380
660
93
372
115
447
449
830
722
406
381
966
220
814
631
23
682
275
515
206
784
132
521
85
605
358
224
128
792
996
684
944
697
989
714
454
811
90
380
392
722
742
991
423
1000
357
900
174
403
507
733
403
3
929
991
819
364
49
420
518
357
37
643
767
554
361
656
520
303
657
273
544
756
393
566
784
200
964
980
201
345
3
595
92
BubbleSort - 0 steps
636
838
584
836
44
644
186
495
320
680
693
967
299
970
945
435
142
699
787
690
230
792
269
539
583
782
90
501
598
372
563
865
328
459
248
185
681
129
92
64
291
570
430
149
462
329
630
901
86
678
577
437
332
113
33
423
211
464
467
790
943
407
534
105
550
847
309
559
857
115
149
360
305
551
973
45
726
427
539
86
577
518
809
496
673
654
741
903
875
921
610
130
78
900
901
795
872
122
616
827
InsertionSort - 0 steps
107
687
371
821
855
818
812
762
133
50
55
598
432
174
583
610
497
944
66
86
593
642
653
178
983
113
361
776
89
204
441
832
858
929
520
8
882
920
335
543
373
405
961
782
91
865
833
790
998
777
932
936
63
345
635
307
165
779
414
166
932
911
819
591
815
922
242
720
100
952
302
226
960
4
712
428
364
133
33
585
44
562
955
239
577
954
163
116
901
903
55
633
136
225
688
209
787
293
360
603
ShellSort - 0 steps
235
869
786
808
196
535
39
14
828
960
207
84
471
13
579
123
864
905
284
547
464
373
823
776
343
738
767
810
356
242
785
993
229
607
157
642
176
600
29
410
224
178
389
376
696
223
746
995
124
912
616
85
35
351
744
491
136
859
288
955
876
612
447
167
111
814
109
600
500
178
517
918
530
690
197
260
810
18
579
471
728
685
928
416
124
927
887
564
754
616
335
874
673
636
300
777
924
527
860
268
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