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.

101
752
360
654
422
301
381
177
568
519
462
537
435
232
832
157
755
225
195
374
909
373
597
284
688
997
305
820
300
365
923
827
179
675
911
831
989
72
982
976
592
894
791
177
171
593
897
340
903
749
372
402
366
691
265
949
343
880
98
130
38
21
60
592
476
505
333
133
210
92
225
21
942
310
890
421
921
148
371
745
780
512
749
398
588
625
622
791
826
42
166
828
63
94
631
653
638
520
894
491
BubbleSort - 0 steps
174
896
357
645
874
28
584
317
248
119
548
384
43
899
398
702
170
321
745
40
70
551
156
791
807
965
980
13
378
894
920
885
689
433
214
423
337
710
75
392
294
729
568
839
774
882
860
610
400
159
528
614
627
363
921
517
488
427
31
91
925
289
678
113
774
455
445
316
788
398
886
395
312
350
34
112
351
206
91
817
359
719
534
57
107
135
106
789
891
785
805
208
886
940
73
781
914
618
10
61
InsertionSort - 0 steps
771
679
638
631
855
299
934
516
786
798
414
303
360
166
850
260
631
548
564
248
958
473
494
911
194
889
470
601
583
873
759
601
771
215
659
983
214
47
878
333
686
27
776
378
85
161
173
597
487
792
45
294
479
574
28
959
843
364
888
28
439
181
956
17
883
584
31
537
150
97
875
220
799
570
138
826
951
840
167
627
425
712
406
403
879
164
519
496
91
760
363
516
341
160
179
783
947
260
722
454
ShellSort - 0 steps
805
634
800
902
493
84
898
396
444
880
429
648
676
339
138
482
139
300
711
513
451
871
190
834
8
702
125
167
381
643
669
219
324
261
357
552
381
555
814
382
105
459
903
159
544
722
487
720
182
619
946
835
659
177
228
885
874
232
164
70
552
102
63
851
530
61
275
928
379
605
640
870
738
963
616
608
707
235
399
834
29
980
347
348
520
557
381
279
305
173
669
567
843
912
242
857
309
163
572
743
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