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.

126
869
250
215
129
443
942
39
967
107
904
54
380
988
597
615
834
605
146
644
947
713
824
129
394
439
765
556
313
320
15
902
166
683
835
46
485
584
204
650
352
948
2
591
980
936
806
973
339
451
517
786
400
925
175
621
380
353
895
80
657
307
14
425
4
559
671
307
627
531
986
381
250
48
687
131
334
390
744
337
569
930
575
336
338
310
988
774
156
740
699
686
416
429
349
502
244
882
123
641
BubbleSort - 0 steps
101
749
65
277
451
374
528
336
824
217
493
788
701
347
77
846
923
722
749
250
930
93
666
643
166
341
442
407
136
223
333
348
48
534
573
272
196
245
204
189
537
999
452
439
966
362
647
719
67
859
23
53
921
3
915
871
337
170
596
92
262
95
591
399
938
673
567
755
185
661
208
29
469
800
379
472
414
478
467
230
491
545
829
707
746
950
276
870
265
858
814
329
655
921
704
989
874
686
71
527
InsertionSort - 0 steps
178
665
432
202
181
121
286
450
992
579
658
890
988
217
788
625
642
908
481
297
222
884
132
145
4
115
473
959
562
712
698
516
161
653
359
228
933
422
604
91
36
289
329
247
148
661
401
574
345
995
424
91
144
999
967
128
300
996
14
178
799
219
190
236
51
724
404
199
214
410
212
359
403
941
403
262
917
421
586
526
60
330
992
756
656
945
109
553
76
642
462
468
147
563
722
134
159
963
107
784
ShellSort - 0 steps
990
390
582
196
357
450
143
931
891
53
123
227
610
386
615
493
815
217
357
711
187
34
908
323
78
253
230
73
96
911
416
369
753
785
860
502
692
423
232
800
8
887
173
865
64
647
784
639
119
606
532
856
892
52
461
30
790
182
459
541
714
328
177
32
585
550
462
266
760
840
531
644
943
157
823
508
617
301
989
205
554
112
516
628
17
300
674
702
649
901
233
847
190
907
467
490
798
820
102
745
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