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.

871
820
591
154
460
134
801
382
601
635
492
211
430
574
516
646
742
666
320
244
298
114
485
968
28
585
737
593
815
640
142
638
502
604
580
885
618
782
593
498
450
435
194
837
339
566
393
668
172
385
899
588
659
685
333
302
524
998
592
862
395
812
309
384
514
158
315
80
990
681
312
173
798
685
121
903
714
629
716
237
797
692
570
210
946
782
474
128
286
312
223
208
694
808
757
100
549
263
829
721
BubbleSort - 0 steps
631
255
14
976
930
414
399
953
189
891
817
460
742
135
388
756
317
949
342
959
627
121
726
744
321
175
371
420
307
220
680
715
851
343
10
641
554
690
994
825
462
341
596
199
227
185
267
587
752
342
21
221
99
667
358
970
775
454
450
869
907
533
634
395
557
618
164
865
512
948
500
660
938
541
329
671
967
389
64
56
398
552
4
574
340
932
343
454
93
709
459
396
486
81
27
650
24
162
338
892
InsertionSort - 0 steps
691
497
142
539
468
748
32
660
554
373
455
21
186
194
279
668
208
8
458
433
205
68
857
19
351
976
665
973
594
818
180
521
885
650
265
161
492
796
574
185
23
170
852
122
983
315
247
335
542
495
895
306
978
306
430
14
11
930
198
856
635
978
883
681
373
445
359
542
358
793
838
395
902
692
533
762
475
634
390
316
299
303
635
56
988
497
441
200
88
743
844
666
50
787
787
279
530
969
637
899
ShellSort - 0 steps
931
57
968
721
11
292
184
815
244
468
550
223
452
200
302
613
589
956
246
106
253
853
436
726
290
468
176
51
948
769
947
300
73
536
122
360
241
512
900
470
78
624
924
701
385
798
22
317
82
994
917
115
283
290
307
402
183
615
734
548
741
843
914
59
661
793
627
30
531
338
971
651
382
490
19
30
344
513
279
211
144
606
267
692
642
792
468
607
164
719
796
457
936
866
362
894
66
473
67
579
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