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.

583
457
560
260
979
687
212
752
637
842
281
192
469
526
403
238
141
594
456
825
982
502
891
91
155
932
506
647
973
395
910
179
539
891
922
109
934
607
285
793
692
4
915
855
795
865
401
244
467
435
473
104
102
860
901
77
158
679
585
900
472
121
292
592
129
953
647
173
161
483
491
99
817
627
449
273
365
497
648
287
186
531
408
404
765
281
50
481
185
995
361
762
788
511
638
945
317
900
583
833
BubbleSort - 0 steps
862
712
279
606
75
427
146
807
291
877
560
448
971
911
526
864
108
133
13
111
623
678
526
721
725
898
57
259
760
689
915
8
187
230
143
375
81
114
802
170
781
297
186
681
114
788
963
432
220
768
233
617
181
96
229
678
643
896
439
445
361
789
478
899
118
712
268
919
225
599
553
49
94
596
40
301
698
743
985
235
546
957
55
97
592
711
454
943
132
264
500
983
422
356
438
748
735
236
466
449
InsertionSort - 0 steps
245
442
531
371
554
780
511
112
973
51
961
50
727
20
365
497
123
210
679
388
748
13
21
541
192
88
390
816
369
630
966
752
817
508
817
832
824
330
297
550
410
67
956
214
593
855
386
670
59
610
617
365
233
395
104
692
942
709
175
746
892
277
352
690
270
179
574
87
606
962
832
107
870
327
989
5
679
807
394
827
867
979
382
424
452
56
264
872
918
706
917
242
535
85
857
481
926
985
975
58
ShellSort - 0 steps
217
889
10
242
345
683
255
489
118
74
817
243
780
961
951
206
678
44
443
357
525
385
833
879
482
817
481
976
300
592
393
771
648
332
736
147
177
844
659
639
609
229
15
861
50
454
650
747
313
112
425
756
884
138
510
697
339
837
712
18
664
486
247
16
845
422
970
712
923
158
529
745
502
990
548
959
396
684
56
859
326
477
781
808
261
347
258
953
125
427
97
57
877
994
711
586
617
601
981
202
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