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.

539
45
997
954
735
159
945
772
231
113
747
171
382
422
439
930
512
823
716
632
753
885
757
515
897
300
162
383
239
33
456
54
251
145
415
674
68
570
161
402
154
44
389
242
429
240
490
590
571
958
774
473
832
650
666
368
391
937
564
566
530
713
905
774
461
419
989
402
639
446
634
981
761
44
493
815
748
979
828
888
653
462
746
827
820
970
972
83
907
276
645
680
237
894
727
45
520
263
515
284
BubbleSort - 0 steps
673
169
352
725
816
558
849
465
696
402
831
978
876
998
400
892
833
406
561
899
417
75
609
498
396
183
685
899
584
53
667
233
817
486
834
568
285
578
73
891
669
830
726
355
590
48
749
663
338
665
467
588
392
251
351
995
840
521
515
896
92
147
694
346
641
691
654
66
802
121
859
959
166
244
892
156
902
956
46
965
362
723
716
290
837
330
816
129
263
216
217
286
725
544
27
953
934
26
603
66
InsertionSort - 0 steps
151
272
318
184
134
324
833
912
913
856
712
343
901
397
777
413
459
428
851
507
970
851
106
66
163
341
299
922
523
173
267
115
157
336
149
995
866
6
866
130
385
610
9
723
921
666
358
261
247
280
4
403
47
298
820
480
349
527
155
768
125
945
841
562
802
343
960
681
693
863
511
246
30
658
730
313
745
187
214
982
677
408
695
778
141
419
18
10
339
737
780
982
629
684
338
814
46
588
552
423
ShellSort - 0 steps
127
103
577
409
955
770
740
203
321
428
51
480
507
773
135
735
292
54
156
817
186
327
932
1000
698
489
410
141
954
73
241
354
571
301
251
450
202
221
392
118
257
337
494
703
961
4
326
787
695
99
51
117
811
785
192
453
157
489
500
877
431
410
151
262
507
669
788
730
5
714
799
240
543
763
129
187
547
563
883
592
182
469
13
630
860
612
268
865
242
605
698
91
481
315
561
161
470
468
874
592
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