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.

298
917
667
381
689
467
222
223
124
517
238
828
554
339
832
644
946
87
889
233
312
604
610
46
933
672
454
275
276
729
508
909
426
983
126
35
41
943
577
710
873
40
146
802
854
672
943
841
799
582
17
306
879
581
8
737
977
666
581
478
359
86
677
278
264
36
818
938
644
709
566
76
725
962
431
935
29
141
675
693
869
960
504
937
887
819
575
58
614
976
547
797
996
616
261
827
493
953
928
66
BubbleSort - 0 steps
43
772
629
784
842
162
933
198
996
448
597
923
83
428
270
884
489
259
368
61
336
301
802
4
620
205
612
637
414
730
29
682
153
202
644
394
949
708
539
566
168
402
219
169
542
90
259
351
146
914
956
986
568
594
524
996
331
518
643
472
556
244
577
131
892
618
33
239
883
621
322
709
659
115
414
185
827
52
129
921
40
546
681
664
226
230
591
830
196
712
747
886
987
872
863
331
972
347
223
47
InsertionSort - 0 steps
130
497
406
178
450
141
878
930
378
960
466
265
531
111
721
371
27
456
721
129
289
929
438
849
429
655
82
438
488
494
420
377
506
799
310
907
724
967
324
354
413
27
485
397
492
533
97
223
854
261
591
737
941
838
26
292
895
469
5
577
59
105
659
256
669
708
450
128
196
859
82
391
201
302
590
309
666
266
498
526
743
881
188
418
987
652
667
37
929
217
290
959
828
348
292
379
267
892
876
40
ShellSort - 0 steps
69
978
731
660
717
977
183
290
568
953
586
289
608
694
653
869
645
977
681
595
463
9
100
620
326
644
930
202
254
407
819
95
126
970
943
735
528
608
779
421
261
9
403
971
4
120
252
144
44
901
449
589
162
263
304
62
146
676
532
93
26
439
832
717
939
26
17
911
666
223
348
543
1000
172
556
951
482
446
923
210
48
170
666
236
678
965
454
752
190
723
395
873
382
18
475
157
641
312
853
512
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