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.

606
259
596
416
10
303
465
546
374
761
262
517
193
869
321
612
274
641
198
120
437
228
36
628
101
858
516
69
511
435
405
163
105
472
865
725
525
254
494
43
151
446
155
112
413
947
35
349
321
94
220
292
864
900
763
236
469
759
803
212
571
47
902
336
508
511
595
249
990
239
766
934
308
727
399
264
894
686
634
666
959
565
789
449
118
204
523
409
817
366
69
724
970
469
139
432
293
50
911
427
BubbleSort - 0 steps
942
565
101
789
897
32
870
965
416
827
790
622
679
343
501
930
95
103
920
484
167
152
219
619
170
326
197
53
574
703
224
114
306
540
37
921
443
899
156
761
42
963
530
435
223
666
644
534
621
773
320
225
474
165
392
727
659
121
224
805
161
257
255
69
542
722
727
776
470
932
463
976
551
573
769
231
276
209
602
960
782
965
131
185
917
137
234
205
314
527
206
304
264
531
309
511
93
928
349
936
InsertionSort - 0 steps
326
383
107
535
879
296
338
727
187
570
784
477
676
80
272
264
232
358
862
898
142
156
616
921
832
807
807
888
524
907
727
487
800
926
997
752
581
638
84
970
16
601
50
2
304
844
242
64
703
242
498
373
325
223
571
556
706
807
757
421
410
889
780
821
142
747
432
646
922
608
57
989
29
216
45
854
599
846
660
755
57
73
290
128
112
884
550
18
437
720
462
163
269
855
510
985
249
394
772
44
ShellSort - 0 steps
754
956
907
792
286
42
384
824
285
627
385
814
738
269
195
561
259
273
697
425
621
690
128
648
353
842
259
422
658
929
930
410
672
837
554
647
888
823
694
94
758
670
507
224
887
512
295
554
741
500
236
60
503
145
909
23
921
908
265
973
624
597
953
233
770
275
900
823
799
861
933
90
599
276
874
365
999
562
268
5
27
699
467
170
625
565
855
530
750
465
963
6
518
84
454
849
561
257
917
858
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