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.

275
195
385
693
302
960
138
814
745
357
848
987
112
907
261
981
298
719
20
782
929
372
243
390
827
174
707
415
145
835
33
140
561
145
227
81
582
564
577
476
692
997
290
83
504
440
166
933
750
101
300
476
53
437
666
173
818
75
583
568
629
839
780
242
22
899
278
762
214
832
298
907
378
321
530
913
137
912
250
464
344
691
911
63
30
665
199
800
709
924
923
839
440
263
752
507
644
346
710
619
BubbleSort - 0 steps
238
440
146
153
800
477
956
813
876
423
995
561
256
433
53
123
464
721
862
743
613
178
463
904
751
398
593
671
13
513
552
771
865
326
749
390
435
979
200
403
401
930
258
567
702
658
315
946
249
924
41
627
837
146
726
872
284
509
624
919
427
953
564
677
140
861
346
251
370
86
780
552
352
788
902
271
621
321
730
277
424
756
648
833
35
659
696
636
780
414
661
510
895
754
76
204
29
23
896
710
InsertionSort - 0 steps
623
580
76
149
290
602
467
443
41
301
498
40
709
472
951
314
485
386
654
453
931
426
610
718
41
591
788
104
905
636
303
169
695
385
42
544
427
512
703
538
962
786
460
126
75
210
290
411
40
973
331
43
731
407
646
646
664
691
75
471
555
259
279
203
839
401
756
552
753
708
660
985
165
181
521
945
227
604
616
179
806
723
838
589
953
14
223
168
399
154
921
929
846
878
200
494
810
382
849
236
ShellSort - 0 steps
593
978
812
190
9
790
897
863
182
213
551
92
921
287
125
944
3
597
177
351
139
462
741
905
482
87
631
548
601
294
44
192
531
533
650
976
602
647
500
887
46
551
483
279
46
411
931
610
427
821
250
241
983
896
431
187
117
899
9
314
785
103
209
699
15
726
308
720
785
403
932
148
129
82
227
430
415
472
219
264
558
319
262
966
815
129
760
511
856
158
535
747
954
536
759
126
879
120
59
706
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