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.

580
426
184
386
886
894
871
802
359
88
265
798
687
415
329
431
949
955
722
250
555
856
607
499
441
968
799
31
407
190
312
810
207
446
339
705
588
472
331
364
633
381
941
584
255
527
227
156
237
81
657
281
310
14
463
913
186
659
641
403
449
621
207
380
529
876
615
989
636
870
133
999
163
214
90
715
846
811
232
224
927
959
28
652
58
103
461
957
580
630
945
882
887
946
770
894
35
933
556
509
BubbleSort - 0 steps
981
581
370
874
199
759
916
224
589
950
83
193
126
834
106
818
975
594
75
833
14
436
813
719
494
407
521
827
568
807
845
773
293
629
589
10
575
968
211
90
228
977
508
669
863
987
992
829
421
415
677
745
710
964
478
85
36
971
679
757
500
306
627
37
154
46
383
35
582
443
982
391
151
213
266
133
66
215
362
362
278
486
204
618
141
127
765
270
484
200
458
23
444
311
47
241
991
41
571
473
InsertionSort - 0 steps
250
34
450
532
151
211
609
449
134
371
558
478
934
970
451
383
267
896
944
969
843
38
959
624
654
399
982
308
258
207
221
389
352
378
52
596
8
850
741
646
668
443
282
599
519
101
264
857
23
789
291
433
802
581
528
728
794
496
278
578
640
789
400
332
69
381
902
718
57
997
731
786
765
828
189
475
961
637
443
675
439
190
104
658
136
563
569
895
147
481
262
56
171
670
302
527
910
320
349
433
ShellSort - 0 steps
272
559
721
893
170
950
158
954
645
313
807
448
301
549
892
392
716
223
824
328
68
708
142
362
696
742
581
190
103
646
887
6
849
722
674
365
712
524
792
625
104
253
479
672
758
231
912
232
337
488
637
445
573
708
967
432
516
836
604
853
937
86
684
857
508
682
181
567
570
504
218
676
629
891
605
39
397
231
568
976
683
861
658
319
767
8
910
849
295
315
985
95
608
187
421
676
410
636
997
787
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