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.

801
629
812
573
777
264
599
131
961
527
282
119
176
41
986
14
105
452
866
144
975
27
980
461
246
537
394
68
17
336
455
118
619
912
83
822
47
633
442
820
369
131
924
786
997
577
228
866
767
286
375
63
759
912
848
959
908
480
968
195
996
469
195
925
639
849
16
901
276
147
926
312
609
720
509
474
198
849
592
813
46
536
230
455
664
583
879
714
966
750
933
113
22
538
354
296
998
111
312
113
BubbleSort - 0 steps
789
814
107
132
699
541
392
284
313
902
336
552
571
978
221
396
315
578
51
489
44
135
69
521
798
874
394
46
468
310
657
828
408
32
122
83
526
39
497
460
272
805
493
196
296
849
165
937
721
692
774
749
660
916
10
492
685
933
135
989
438
707
578
563
686
769
379
98
167
403
16
713
877
556
834
654
324
906
324
289
396
697
267
170
684
484
418
307
676
419
947
921
52
156
464
956
209
256
347
379
InsertionSort - 0 steps
754
35
100
451
188
712
473
35
949
461
78
898
922
207
954
330
332
570
952
686
919
91
421
977
381
286
646
265
473
160
183
175
490
421
578
587
793
755
561
420
836
146
396
679
47
1000
308
839
810
653
292
192
232
934
393
102
675
565
760
39
695
355
265
732
296
15
708
959
61
89
615
122
528
932
42
990
288
895
562
827
285
1
544
912
42
98
489
957
19
796
628
972
434
373
861
990
921
688
348
278
ShellSort - 0 steps
81
802
801
13
13
514
533
903
654
568
437
938
170
80
715
509
803
653
336
588
956
620
885
768
438
579
357
868
163
287
646
324
313
158
216
8
328
774
529
506
93
590
655
188
714
669
637
918
183
401
318
649
220
510
102
161
975
33
501
744
61
541
696
304
314
738
904
1000
403
929
307
863
186
485
729
674
906
803
432
76
660
897
679
412
466
667
227
78
34
20
310
748
274
823
901
35
355
340
930
363
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