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.

839
538
837
139
778
312
108
16
64
746
989
744
936
910
10
758
620
719
664
688
748
984
496
425
666
339
855
344
922
796
967
522
910
381
975
211
126
447
152
159
830
403
791
393
835
807
771
213
705
511
657
479
290
154
92
509
157
122
843
540
691
940
946
779
587
600
662
690
474
535
749
213
183
62
762
246
355
102
150
571
708
22
905
411
914
194
467
698
382
89
332
84
286
600
271
816
432
1000
114
675
BubbleSort - 0 steps
942
345
344
339
573
270
157
658
360
346
901
669
128
487
638
8
416
869
182
211
357
864
706
908
322
789
610
887
211
528
876
482
813
62
538
473
309
283
407
673
404
518
267
418
974
495
890
202
389
62
199
78
350
972
593
954
884
846
554
155
486
130
716
278
454
292
615
171
423
695
398
598
529
653
169
609
505
173
522
700
835
531
394
96
705
871
443
368
921
406
708
290
166
470
404
366
277
532
171
871
InsertionSort - 0 steps
321
984
979
322
432
719
72
138
810
911
216
613
286
608
694
724
349
803
180
655
278
875
610
746
582
798
920
909
533
815
168
440
282
605
379
860
106
54
513
587
969
25
853
298
701
944
97
175
799
566
80
564
384
805
136
838
664
831
558
95
953
310
223
567
759
250
25
48
840
421
343
267
177
685
820
478
46
307
399
371
507
705
995
257
542
789
948
156
755
693
354
201
648
258
756
524
421
874
615
600
ShellSort - 0 steps
615
359
647
550
90
743
708
929
268
117
52
352
13
679
19
408
342
702
900
162
400
724
379
732
565
663
567
535
710
462
694
955
984
995
227
311
4
414
218
301
421
819
172
378
599
29
95
526
813
356
431
221
839
127
801
411
663
99
857
350
80
459
749
895
477
900
466
786
950
960
672
695
941
167
593
697
873
197
217
444
335
772
309
991
134
294
442
354
891
950
966
90
44
963
360
394
286
415
139
909
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