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.

5
374
39
111
781
722
913
14
128
17
896
745
219
38
540
822
89
284
673
211
340
200
864
536
182
230
956
989
442
705
998
316
586
683
933
81
450
355
787
419
219
67
693
965
760
722
169
975
298
989
839
214
544
137
353
997
772
850
127
969
395
823
403
42
430
158
946
461
766
638
189
759
816
860
736
123
253
710
221
118
415
941
279
137
331
426
246
690
587
299
505
694
972
459
950
73
23
498
851
619
BubbleSort - 0 steps
700
190
323
92
221
614
734
574
295
914
424
404
849
993
519
16
530
136
97
874
79
17
23
831
708
831
897
284
558
213
907
794
474
569
397
114
853
506
819
895
855
91
498
110
508
885
144
84
154
853
13
837
670
865
640
407
913
4
552
710
951
707
637
756
794
215
509
156
344
39
522
637
831
573
698
126
706
802
628
267
821
182
941
986
590
137
80
187
32
744
518
328
421
43
301
40
873
587
710
620
InsertionSort - 0 steps
86
838
89
905
676
807
911
382
599
641
741
675
957
251
109
49
438
566
913
513
1000
666
658
844
236
161
512
529
507
353
983
241
704
945
722
342
144
615
787
655
148
405
316
70
400
266
721
254
455
526
106
556
754
198
107
409
737
48
42
599
54
148
101
106
38
588
496
559
868
54
517
473
85
50
747
255
256
998
891
846
814
808
259
715
744
24
786
464
324
242
842
390
627
597
380
960
439
787
849
300
ShellSort - 0 steps
734
992
318
7
698
351
378
356
673
235
804
60
187
219
375
468
584
516
259
100
513
921
595
661
477
266
14
741
649
121
470
633
318
906
50
436
831
681
407
883
864
172
70
448
521
472
459
658
814
198
51
715
592
767
449
236
503
941
528
540
7
804
214
104
962
824
999
741
540
955
716
407
155
725
292
921
737
193
373
31
714
155
87
505
780
339
891
336
354
478
214
497
726
524
825
141
397
777
784
54
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