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.

470
879
25
1000
716
643
302
166
58
83
493
216
661
345
12
960
477
944
418
566
356
536
495
407
820
441
848
694
189
211
655
535
425
182
295
400
1
279
674
516
166
581
657
402
3
756
338
765
904
276
734
652
992
850
923
242
233
661
348
899
25
365
126
246
737
640
368
411
891
798
854
12
192
980
679
153
161
969
301
85
748
291
384
154
112
66
643
238
819
420
787
874
249
700
788
502
919
637
975
734
BubbleSort - 0 steps
209
151
119
636
441
850
14
94
729
677
786
265
970
743
166
277
602
442
302
346
807
934
782
242
712
901
488
928
318
786
790
510
166
232
868
96
326
628
604
526
480
777
208
350
233
326
749
845
940
811
495
598
225
699
768
7
344
45
602
453
306
159
900
431
945
559
140
552
112
990
402
927
906
703
49
479
767
646
670
512
208
571
705
345
881
880
944
85
641
187
693
919
976
587
825
419
955
830
661
56
InsertionSort - 0 steps
886
308
462
369
683
908
50
769
768
508
575
474
187
256
402
916
527
700
350
834
765
627
217
44
928
335
559
716
345
134
446
1
559
255
199
894
697
114
850
447
973
125
301
967
146
434
454
954
751
815
455
486
300
975
833
364
764
381
448
549
118
556
962
110
57
23
70
695
667
944
103
539
369
93
159
274
355
491
124
379
685
117
453
251
176
905
544
762
573
806
607
93
249
364
397
678
323
31
543
933
ShellSort - 0 steps
428
439
918
521
119
614
662
365
634
746
186
29
117
982
362
933
623
348
909
962
268
172
58
535
473
570
655
478
941
58
956
850
124
274
694
196
326
316
133
948
424
805
987
54
519
285
63
527
774
791
797
633
845
12
243
291
944
657
589
961
741
684
312
322
439
83
553
624
606
669
948
822
654
887
277
244
732
980
409
250
943
763
84
794
63
333
445
538
177
685
657
21
32
793
617
296
485
517
751
50
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