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.

309
900
687
880
813
459
138
258
443
143
805
315
32
425
234
811
408
744
692
128
822
183
338
351
526
897
101
409
814
431
310
717
172
355
342
656
632
985
244
161
456
45
577
126
844
115
182
276
321
537
386
517
854
938
50
855
893
861
965
242
664
555
431
798
400
964
303
770
697
297
273
837
782
919
747
761
841
818
579
17
204
890
812
684
370
376
971
612
427
279
349
791
754
745
76
400
154
92
393
85
BubbleSort - 0 steps
894
975
351
360
48
286
987
979
887
37
938
650
960
814
948
798
708
248
940
743
179
952
772
216
959
910
669
134
159
817
792
783
972
700
800
921
786
775
766
959
571
997
599
890
16
455
938
978
819
794
820
668
656
435
769
151
565
1
310
617
770
600
286
636
953
240
187
259
56
555
175
171
914
445
881
659
748
412
928
986
65
536
8
265
608
441
561
410
631
899
53
484
121
23
252
389
825
720
718
570
InsertionSort - 0 steps
800
605
78
457
537
254
290
876
691
301
386
647
780
562
984
747
82
951
174
589
229
437
44
456
220
269
144
953
512
882
936
770
868
389
628
347
528
500
264
867
487
656
379
780
500
545
960
732
717
289
708
583
363
78
965
544
444
184
734
514
432
685
120
247
79
551
296
956
792
491
469
513
70
784
823
165
336
709
825
755
344
544
790
316
350
306
527
289
245
202
114
211
188
580
258
393
45
164
549
180
ShellSort - 0 steps
264
489
16
611
956
513
763
276
671
622
332
279
579
861
528
439
345
421
770
830
117
571
721
619
935
529
750
233
348
331
119
750
174
11
831
843
102
181
511
981
349
289
272
920
716
739
111
162
841
957
415
650
248
308
810
57
972
763
175
835
158
883
258
631
48
167
858
92
921
219
577
736
804
267
886
608
602
97
997
869
725
41
530
194
315
98
504
672
392
795
189
96
407
148
292
958
546
453
67
237
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