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.

669
569
815
697
534
962
359
271
608
75
864
16
455
156
277
664
467
667
954
878
299
378
726
739
448
639
299
46
612
514
451
392
181
218
894
678
469
201
137
818
44
949
947
452
488
22
348
817
242
277
271
434
121
176
111
144
995
435
60
641
421
505
570
926
542
206
521
207
978
247
423
872
233
655
60
282
154
705
304
991
671
628
621
471
784
821
734
436
194
833
207
319
747
468
954
569
294
151
58
641
BubbleSort - 0 steps
476
817
19
421
467
580
52
10
467
264
53
941
453
463
393
316
308
63
450
308
622
390
839
761
70
631
603
107
528
928
610
836
464
552
200
648
396
423
236
936
95
887
578
780
853
815
300
595
496
649
160
536
961
879
941
595
586
756
317
448
388
10
573
586
362
996
203
205
338
403
414
83
556
204
424
268
651
826
694
879
164
217
316
807
87
434
212
769
612
647
197
654
968
174
310
250
817
771
19
182
InsertionSort - 0 steps
697
783
474
626
946
949
868
593
996
593
676
506
466
557
13
56
242
559
33
633
793
946
180
197
68
785
403
615
43
716
418
901
140
856
569
905
24
739
873
816
974
549
456
960
270
348
919
813
677
794
737
607
973
562
506
76
300
766
410
80
175
6
193
741
2
974
116
255
841
769
837
188
653
512
919
80
568
981
525
208
368
470
451
992
274
589
635
361
150
415
625
466
590
863
265
315
57
193
972
719
ShellSort - 0 steps
200
820
641
636
116
584
965
979
932
476
352
649
849
789
964
716
43
133
764
236
448
51
288
230
769
413
508
797
822
811
824
630
78
574
878
154
603
791
319
340
466
957
319
879
894
405
34
767
105
983
273
261
771
463
914
587
955
478
452
643
431
268
804
982
393
303
402
990
465
817
342
135
719
558
935
793
876
277
846
531
997
435
181
794
10
579
548
68
399
632
534
54
887
911
268
67
158
36
628
636
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