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.

633
919
646
928
259
703
838
34
523
231
118
331
5
257
555
620
393
376
982
985
656
275
756
121
976
236
740
876
104
617
934
220
909
162
776
111
54
458
365
391
214
548
634
9
976
366
277
758
791
529
244
874
316
555
662
738
793
671
980
436
68
308
907
263
704
568
219
83
623
396
932
95
146
300
579
854
94
619
220
664
588
718
308
710
261
186
659
850
194
293
794
459
196
59
346
370
299
891
688
691
BubbleSort - 0 steps
366
144
416
577
214
777
491
894
719
764
11
313
407
509
532
386
986
314
164
87
426
174
75
385
912
915
547
211
242
135
524
838
442
514
83
611
860
344
237
723
196
515
661
360
57
227
78
280
916
758
755
43
404
997
275
355
992
856
624
179
989
745
497
881
273
368
297
987
676
139
295
747
755
674
668
956
105
498
168
610
232
946
124
581
395
987
106
591
427
937
418
299
752
506
528
265
1
238
136
980
InsertionSort - 0 steps
754
443
332
271
459
889
374
316
890
407
127
127
195
384
180
923
950
955
232
795
973
332
127
808
202
22
558
804
655
316
523
299
70
164
445
378
574
370
440
188
430
706
465
762
912
262
54
662
637
792
99
665
30
434
13
66
728
437
540
553
11
140
12
528
929
467
484
819
214
813
936
917
661
147
206
403
931
352
649
988
15
705
376
171
22
448
74
728
23
339
483
804
110
275
302
993
202
553
380
699
ShellSort - 0 steps
250
28
926
924
173
359
450
182
854
646
604
322
780
923
626
994
23
999
71
286
42
727
2
716
160
857
88
413
595
335
907
948
135
559
115
654
354
666
833
727
854
656
593
958
15
598
495
796
854
170
994
629
348
525
647
184
935
948
371
815
759
664
442
795
954
138
167
254
395
838
698
610
88
347
700
27
861
976
207
262
823
750
983
565
4
399
429
387
870
31
770
860
238
744
364
742
234
209
968
837
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