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.

928
996
149
24
885
289
825
915
737
979
397
338
541
615
143
493
831
151
301
832
272
513
176
658
244
608
280
754
864
375
668
473
364
589
376
139
902
228
827
113
514
72
709
583
36
193
675
156
632
625
19
333
315
632
319
47
784
494
77
71
434
471
385
854
10
147
731
947
466
518
311
200
457
688
907
31
637
608
326
436
949
510
564
852
5
686
13
999
884
195
707
240
943
392
63
289
518
940
444
904
BubbleSort - 0 steps
842
195
903
13
56
965
72
730
848
986
42
468
647
414
191
520
578
311
215
862
101
799
700
923
642
835
703
796
734
787
147
299
17
898
479
675
555
825
176
288
594
276
919
668
111
270
801
75
741
888
344
19
610
94
47
610
369
770
255
816
747
195
834
36
58
922
586
78
176
669
843
637
826
331
938
848
87
918
78
63
213
848
672
897
215
803
219
151
688
745
818
151
65
223
871
686
585
792
759
943
InsertionSort - 0 steps
520
780
98
19
364
75
128
404
719
364
489
642
455
452
519
540
567
884
702
628
933
464
874
364
607
646
808
501
855
8
303
1000
502
865
250
342
610
285
816
700
836
722
478
154
382
348
74
663
867
810
44
311
861
683
90
143
837
573
501
734
695
396
798
838
608
334
241
736
733
333
108
86
825
231
50
113
553
657
318
251
955
802
935
371
316
244
142
288
733
533
582
386
526
759
794
875
405
527
231
927
ShellSort - 0 steps
822
282
197
258
36
910
129
518
970
752
220
85
89
197
511
842
191
540
205
690
692
852
525
898
323
507
904
120
222
826
565
743
760
32
243
442
632
948
518
106
582
911
390
203
67
590
960
883
195
97
590
445
562
653
838
147
978
440
43
977
281
787
811
528
16
354
354
504
439
989
127
411
371
598
706
106
122
67
875
908
467
424
881
747
834
242
712
313
264
297
221
745
948
789
371
3
665
853
456
255
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