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.

384
475
632
995
287
343
587
830
736
281
166
870
731
41
416
738
993
871
128
524
722
981
134
519
3
444
446
389
341
524
509
693
999
148
44
836
141
643
602
826
435
186
712
327
328
22
380
860
410
628
58
404
726
75
703
294
859
81
56
411
254
860
126
707
895
321
277
880
478
763
568
851
185
205
816
500
126
696
887
477
239
655
50
454
983
882
236
311
431
906
635
39
892
928
406
358
950
607
70
837
BubbleSort - 0 steps
78
883
656
60
482
945
702
485
859
617
626
467
628
298
625
32
902
765
183
622
308
663
149
350
286
306
562
154
758
838
222
8
127
231
288
20
524
567
301
292
576
620
526
175
44
808
504
773
375
29
452
130
474
886
223
454
35
686
31
168
403
641
466
730
770
510
369
472
901
526
529
405
61
5
231
899
807
117
844
231
191
438
862
527
833
79
263
534
87
987
943
995
990
217
230
12
920
906
921
788
InsertionSort - 0 steps
869
547
325
658
20
629
388
515
722
798
132
575
980
92
999
254
953
892
327
705
311
72
852
440
700
249
924
278
189
657
18
634
96
752
379
402
302
435
379
24
406
363
411
322
508
606
976
715
550
654
102
374
766
512
581
800
973
373
917
844
408
817
447
147
867
960
844
383
58
880
959
261
724
704
863
731
733
531
981
622
632
617
302
580
193
637
712
816
464
237
57
961
451
886
875
62
813
996
468
786
ShellSort - 0 steps
159
693
579
88
389
437
477
623
338
446
253
459
264
934
118
708
705
912
229
618
299
621
946
247
249
815
599
743
424
29
985
763
655
911
628
540
260
159
470
917
714
428
915
113
859
919
587
9
600
591
218
591
330
28
30
582
15
610
521
186
404
579
320
193
346
347
150
247
573
640
98
694
75
970
662
423
587
738
927
99
604
604
512
317
4
553
194
276
737
66
864
927
731
473
189
837
197
607
848
179
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