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.

33
711
426
833
765
717
817
496
514
783
516
226
495
980
342
991
25
640
322
719
617
897
685
295
157
529
944
210
899
237
367
923
927
478
802
738
529
499
202
119
28
137
475
147
877
367
908
8
16
977
67
681
472
908
857
191
560
995
740
948
87
879
886
535
254
159
918
594
291
645
694
662
558
757
396
312
612
669
843
868
863
525
136
596
763
273
119
46
909
411
817
400
320
117
846
712
221
232
467
980
BubbleSort - 0 steps
555
58
325
668
211
457
318
194
661
69
691
927
296
966
849
567
263
658
319
883
889
888
358
907
381
326
698
640
290
274
596
181
526
184
514
431
838
965
686
381
605
322
552
505
828
421
989
14
750
121
576
452
58
583
216
791
446
949
853
930
450
368
990
230
86
302
538
334
272
517
440
235
657
443
332
426
60
978
566
192
923
342
894
743
1000
697
370
18
775
701
422
646
401
305
346
208
157
868
185
428
InsertionSort - 0 steps
84
59
511
963
24
166
154
350
92
985
726
100
658
857
982
728
399
762
253
968
163
989
979
542
772
54
97
570
167
116
414
213
134
477
551
815
371
348
491
758
78
498
605
347
5
779
66
913
955
977
301
845
539
940
285
57
358
492
756
853
386
570
12
50
786
845
426
580
719
457
648
473
185
597
923
36
736
933
998
764
629
159
5
8
744
272
506
841
908
917
904
734
554
150
655
229
211
79
627
598
ShellSort - 0 steps
80
805
648
856
85
752
817
967
843
374
585
393
737
85
661
714
387
410
46
714
705
394
701
531
484
377
360
250
312
584
293
181
698
711
455
281
847
604
86
902
742
592
4
725
919
989
146
104
965
125
539
629
414
847
44
666
559
265
911
353
77
76
502
550
55
776
492
266
943
342
912
295
297
108
581
375
530
370
413
544
586
907
516
610
163
53
647
931
998
630
584
244
828
877
786
4
887
525
738
71
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