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.

967
877
381
103
883
768
712
9
86
371
610
815
830
802
489
895
180
479
533
863
737
994
684
746
46
573
594
620
744
577
891
124
834
718
129
126
978
656
497
25
980
688
364
644
277
887
206
68
645
519
641
328
880
639
72
196
684
713
98
651
74
237
690
673
214
170
598
634
510
962
150
785
880
551
880
823
399
163
258
818
452
219
941
955
931
528
1000
923
7
605
899
910
821
955
155
92
760
294
472
709
BubbleSort - 0 steps
108
482
817
90
634
46
388
535
215
383
895
665
577
226
197
598
154
84
873
520
436
120
512
316
3
888
710
433
287
766
731
10
955
882
802
168
690
235
136
757
602
83
404
449
803
582
808
110
394
655
781
826
624
553
492
565
973
934
669
189
874
418
861
868
256
523
213
150
945
361
151
917
460
717
8
645
312
488
465
355
93
931
850
613
179
426
153
339
886
810
394
87
740
364
207
95
284
748
220
541
InsertionSort - 0 steps
931
508
210
291
549
416
702
831
386
680
820
791
421
201
929
199
134
438
169
21
945
491
490
438
705
31
397
826
838
535
269
755
870
332
20
594
736
272
69
309
893
152
927
924
108
260
238
433
127
932
560
339
126
883
738
441
946
925
31
451
754
789
336
325
53
697
574
820
539
381
183
667
925
995
390
542
316
997
408
942
596
169
863
10
19
107
634
688
475
438
515
327
753
151
106
907
552
123
372
519
ShellSort - 0 steps
268
484
461
975
922
484
519
491
491
99
135
983
832
274
20
905
380
286
723
812
485
554
325
517
395
783
588
467
541
220
219
160
857
206
947
728
53
496
56
144
657
211
414
763
405
315
399
849
564
284
534
767
486
33
646
372
31
917
114
362
912
438
96
344
914
875
639
525
711
803
872
937
193
549
998
499
194
998
350
552
639
889
897
742
93
585
221
956
781
459
432
877
282
50
307
837
829
706
835
744
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