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.

99
273
577
335
568
390
55
584
944
822
683
835
750
74
961
490
104
628
310
825
721
370
502
509
87
758
461
444
865
312
602
769
58
730
288
496
111
277
803
305
652
905
878
888
356
273
747
510
716
709
614
756
375
401
150
759
130
945
94
162
328
584
894
206
198
863
732
237
464
614
167
127
428
134
827
352
615
770
699
745
543
467
520
752
715
439
443
407
661
441
277
125
194
693
795
306
740
633
140
776
BubbleSort - 0 steps
456
740
732
963
484
545
330
391
428
469
145
536
767
635
865
974
334
951
953
314
597
580
993
58
66
468
337
846
119
191
406
919
340
618
545
241
363
493
897
116
630
165
768
850
358
454
95
300
84
496
341
621
793
123
385
903
744
831
154
427
276
771
147
524
52
317
555
521
913
974
516
656
199
311
842
896
913
103
912
606
859
215
977
153
690
892
214
24
472
169
225
465
279
976
665
695
513
230
265
944
InsertionSort - 0 steps
740
300
479
183
293
741
517
54
670
941
680
398
618
703
217
829
10
886
783
166
331
768
663
980
975
970
657
228
435
116
483
848
338
655
23
494
193
4
821
487
436
356
483
974
969
124
517
799
880
86
673
589
913
92
633
905
750
301
836
134
142
866
588
806
325
912
231
918
921
944
698
552
150
538
812
588
949
750
283
981
765
317
748
901
716
904
254
681
480
750
357
686
16
345
454
435
86
647
791
560
ShellSort - 0 steps
992
991
11
173
629
929
616
804
513
521
604
215
339
179
959
261
889
625
467
96
185
941
822
29
59
480
503
956
434
721
828
788
70
788
187
815
564
402
581
491
486
256
710
725
993
359
466
83
223
523
479
12
656
336
8
408
983
58
148
118
233
478
305
52
226
606
874
411
627
859
635
466
447
646
749
964
88
44
410
873
794
506
784
853
386
742
516
989
238
373
500
800
406
23
232
348
302
658
860
598
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