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.

661
813
540
198
996
854
493
920
927
927
845
189
263
422
526
191
706
775
760
484
229
481
899
967
339
103
502
474
546
814
828
946
655
680
150
979
936
118
717
741
421
55
958
576
13
218
390
878
338
107
325
289
895
504
908
52
394
458
269
666
453
634
148
902
186
933
735
918
330
2
869
314
814
60
748
330
611
125
34
529
897
422
462
887
910
432
260
363
217
595
161
261
445
881
254
966
541
60
541
900
BubbleSort - 0 steps
949
33
370
679
252
898
229
96
151
297
797
1
156
828
899
805
429
478
788
166
768
174
472
662
432
282
471
779
570
806
750
42
974
840
328
498
977
522
319
300
526
411
88
93
848
199
957
294
584
867
486
317
929
533
647
795
866
667
205
615
159
701
824
649
836
969
762
963
416
459
191
274
964
440
364
525
516
77
879
961
293
679
106
592
474
453
920
480
314
508
283
192
180
56
741
923
14
689
29
705
InsertionSort - 0 steps
879
502
943
451
114
111
833
262
810
600
434
797
671
511
811
745
552
537
421
321
291
834
414
570
247
609
149
420
205
999
448
541
567
556
233
73
53
236
329
571
345
791
945
15
353
182
663
238
538
930
196
604
259
90
636
705
254
846
332
384
292
154
468
622
604
958
69
119
246
474
421
895
443
304
327
785
1
557
249
428
466
782
846
392
316
351
583
874
693
64
652
180
670
596
275
636
226
427
107
641
ShellSort - 0 steps
736
916
485
845
664
711
80
453
54
665
384
795
17
240
961
44
876
361
450
955
961
766
706
675
695
298
368
976
284
875
372
546
425
230
393
94
889
960
585
38
228
979
302
222
319
818
467
4
608
248
673
164
625
252
126
362
733
934
54
962
591
644
719
808
813
39
397
924
665
172
513
948
442
719
270
103
881
621
1000
304
220
253
47
773
217
756
572
470
289
265
417
339
574
444
592
208
634
751
589
518
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