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.

784
105
944
220
263
106
481
84
856
364
870
461
101
978
52
205
180
824
810
5
690
571
68
497
396
895
543
427
879
452
541
598
904
837
235
581
323
522
342
506
540
182
652
640
784
835
333
897
150
765
300
381
398
217
379
177
542
785
97
505
482
300
735
30
948
358
643
578
301
153
920
625
707
266
637
893
747
425
203
153
648
632
47
57
658
936
293
213
999
85
910
415
751
933
812
712
567
324
217
797
BubbleSort - 0 steps
14
143
729
206
273
468
382
614
257
407
762
766
429
317
141
540
735
657
705
750
908
674
593
511
96
879
520
940
892
612
572
782
447
560
672
517
364
584
435
905
449
204
866
230
525
582
340
105
950
988
662
133
737
646
717
527
30
37
132
759
361
520
661
248
64
394
93
247
121
649
340
429
983
123
387
867
133
678
393
46
383
943
437
723
214
710
560
246
420
334
930
849
566
599
82
938
700
210
432
743
InsertionSort - 0 steps
862
885
665
584
830
702
783
997
467
548
131
720
464
19
986
566
917
61
464
491
961
716
890
816
717
177
14
102
111
848
993
509
19
381
778
44
960
937
77
169
897
607
21
903
623
745
719
769
534
67
746
141
365
122
381
178
55
201
286
182
202
752
808
71
951
757
698
593
629
88
997
821
89
644
429
234
502
544
591
935
590
509
26
364
977
668
169
658
545
871
184
535
714
29
2
750
598
652
401
789
ShellSort - 0 steps
117
436
782
296
648
624
502
477
869
934
486
932
889
84
72
985
590
80
257
26
934
437
884
476
785
122
267
333
666
203
362
22
560
735
470
725
809
913
620
266
893
24
229
793
99
788
384
578
564
625
16
442
617
457
596
278
640
289
490
550
234
632
936
598
414
957
621
17
743
656
678
396
772
257
841
696
549
192
739
693
975
754
700
582
388
948
61
379
412
168
304
40
854
192
826
706
717
873
830
853
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