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.

657
451
868
472
923
358
590
831
614
547
179
551
790
17
65
202
406
271
595
845
115
897
356
650
883
784
71
980
998
817
777
326
133
776
821
126
921
446
550
2
553
164
841
935
49
329
732
763
4
463
591
535
237
421
921
845
889
389
716
129
853
307
463
281
141
812
636
761
929
522
973
384
401
943
609
574
811
967
227
875
963
198
336
477
560
696
603
898
231
732
51
137
617
370
952
488
777
141
952
696
BubbleSort - 0 steps
304
631
749
661
373
855
7
519
572
906
952
385
387
220
722
524
896
134
648
827
256
695
403
652
745
289
785
463
598
390
997
221
60
326
819
869
220
455
721
250
115
219
674
610
644
154
651
545
572
547
859
411
764
687
12
661
325
923
911
394
762
10
599
62
135
269
909
550
412
343
255
929
702
638
607
213
391
208
826
592
75
461
455
127
607
664
880
653
130
934
618
722
206
54
263
624
313
418
654
846
InsertionSort - 0 steps
986
174
974
255
918
120
760
209
536
793
63
746
944
904
469
250
6
432
867
979
263
109
833
947
204
5
911
95
445
999
610
442
628
567
647
942
872
211
383
451
477
239
444
995
836
353
861
813
115
92
975
937
455
148
438
96
88
25
80
715
37
741
775
962
340
234
374
387
586
941
488
597
655
2
486
87
816
533
430
865
376
195
527
36
230
208
745
85
400
400
894
402
952
350
629
783
730
836
11
131
ShellSort - 0 steps
26
276
655
945
880
277
183
785
143
801
523
416
564
542
489
726
482
839
277
942
27
367
301
407
970
4
467
627
684
527
304
223
735
517
530
404
59
96
763
335
198
765
961
17
793
720
834
845
255
61
17
487
211
319
953
92
97
408
497
992
831
534
45
308
515
882
555
310
988
310
463
150
66
938
472
84
899
349
771
20
964
174
872
869
549
686
821
705
143
293
735
176
507
88
757
273
272
234
784
57
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