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.

609
617
581
810
78
701
549
886
810
956
365
941
607
240
645
304
470
477
622
856
686
882
275
792
673
302
707
525
645
536
619
227
920
961
197
42
698
310
786
797
107
193
663
210
432
920
354
35
806
979
838
122
840
551
850
33
946
73
114
247
85
191
814
186
695
823
629
679
891
566
483
153
214
796
485
990
636
546
677
990
721
475
462
622
557
473
500
66
895
347
34
683
14
325
570
781
936
490
546
682
BubbleSort - 0 steps
331
268
225
272
103
650
135
970
949
204
49
439
622
813
235
742
880
796
55
348
527
593
968
500
474
179
540
349
825
618
315
330
173
917
745
879
366
501
794
494
382
45
326
990
131
778
614
917
769
465
123
804
838
919
874
39
781
57
574
587
569
372
863
784
887
112
285
54
712
985
674
549
307
638
108
794
637
25
337
757
373
955
652
189
208
578
721
121
624
53
767
677
592
217
692
157
344
784
361
169
InsertionSort - 0 steps
476
219
966
282
6
870
808
748
434
850
134
998
653
169
524
186
423
996
439
943
342
41
784
316
981
46
599
543
919
190
533
488
891
617
935
179
235
127
86
293
296
639
221
665
576
394
538
697
350
59
474
175
986
71
309
434
728
187
728
911
744
976
370
942
50
345
347
744
694
547
721
806
417
486
714
958
905
168
732
763
103
840
565
561
626
467
272
930
863
169
687
525
454
262
973
437
926
425
633
64
ShellSort - 0 steps
56
82
75
640
482
725
720
100
79
903
743
12
232
393
790
943
335
696
885
186
774
960
532
647
822
318
448
985
96
647
526
273
106
356
889
245
611
474
920
663
616
70
541
849
631
341
189
808
819
807
743
509
778
535
939
97
203
454
364
475
649
45
479
698
946
580
893
639
478
315
444
115
795
909
164
63
396
68
301
718
942
884
216
740
238
705
929
151
289
558
673
625
608
208
103
110
962
797
834
450
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