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.

200
234
146
904
609
446
37
941
416
963
482
920
814
234
140
989
652
167
58
68
432
7
964
110
303
211
493
317
330
535
261
575
897
354
888
17
337
657
58
663
534
184
169
796
816
860
775
896
786
806
248
459
698
9
619
810
309
494
413
637
970
324
653
88
635
706
424
691
502
399
639
452
927
807
475
464
349
835
133
347
604
851
811
105
372
687
442
981
284
360
604
972
251
615
395
397
931
750
332
767
BubbleSort - 0 steps
771
931
79
610
950
131
476
538
432
646
597
784
674
672
40
74
632
426
948
637
595
404
72
424
751
245
497
637
254
429
113
740
281
989
710
741
366
702
135
563
573
580
690
931
905
412
870
866
409
625
977
234
515
471
855
356
973
105
20
143
944
945
230
389
503
291
694
523
937
975
452
244
367
399
130
966
69
867
480
349
919
975
301
152
463
335
569
890
274
98
331
961
596
212
725
889
856
193
657
722
InsertionSort - 0 steps
98
652
506
152
301
744
863
57
438
440
390
662
220
331
111
488
937
551
113
573
200
757
507
345
511
496
161
331
808
693
476
245
493
73
52
851
670
590
386
13
95
978
613
486
39
652
424
828
673
938
166
287
546
955
174
758
994
617
686
316
371
258
221
739
265
351
955
450
443
143
84
781
903
604
948
707
6
270
464
980
721
967
699
337
963
520
654
710
775
227
785
260
639
46
667
526
926
278
582
233
ShellSort - 0 steps
315
906
622
277
866
329
444
301
683
19
194
602
953
622
699
505
287
119
152
571
725
391
813
804
928
496
807
874
614
930
805
304
157
542
731
302
225
938
358
925
724
575
377
21
786
116
684
379
955
3
930
515
719
493
92
655
505
152
167
800
746
726
549
432
503
246
423
599
951
95
673
231
546
355
122
963
892
50
401
438
596
617
946
325
827
795
331
577
641
626
245
132
406
151
714
342
408
303
372
102
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