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.

782
66
951
575
582
671
454
575
884
990
156
845
24
951
933
893
284
98
882
566
430
915
694
273
696
425
902
10
867
226
61
760
698
756
438
554
234
911
868
585
847
489
73
890
309
865
144
52
257
937
919
799
238
8
645
9
434
23
938
407
154
217
724
240
892
897
613
978
556
272
357
868
702
390
606
130
954
189
210
585
362
880
684
783
270
167
577
848
594
931
424
99
43
231
358
122
601
998
281
499
BubbleSort - 0 steps
570
180
395
220
788
348
338
949
583
259
353
990
703
529
24
800
853
478
299
427
545
43
565
58
279
15
145
257
844
821
839
760
428
469
469
384
948
151
151
922
686
984
34
146
395
530
631
151
703
246
102
57
816
193
789
981
429
290
829
437
949
95
234
133
176
214
752
606
277
908
556
548
443
701
154
967
291
483
196
215
565
787
950
803
221
405
965
430
740
265
302
727
921
263
837
271
539
330
643
49
InsertionSort - 0 steps
562
586
616
690
622
281
453
879
631
745
784
764
416
897
586
908
2
720
542
509
416
679
690
34
382
757
89
362
568
208
457
169
364
340
268
511
230
884
515
937
886
254
695
121
369
143
29
674
362
7
295
330
365
847
798
430
686
243
813
787
244
944
102
53
97
758
764
486
386
888
771
311
729
89
177
398
176
333
37
128
732
111
662
719
102
990
484
614
618
357
643
759
665
545
836
687
414
48
899
148
ShellSort - 0 steps
677
324
832
59
77
873
650
285
332
890
626
235
536
148
247
774
859
626
630
186
582
600
19
612
338
985
542
385
244
413
255
230
108
63
650
434
855
123
976
709
560
992
651
677
633
192
65
880
409
254
99
605
399
442
560
299
664
697
657
184
810
938
471
498
219
437
562
789
769
563
945
76
50
15
22
179
123
872
988
707
35
989
64
948
771
783
871
907
218
246
766
291
244
668
532
528
396
146
847
617
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