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.

28
958
183
412
338
21
173
41
179
582
887
813
435
924
922
132
906
685
731
111
518
458
904
460
849
62
802
972
133
262
377
689
977
23
844
662
526
885
278
541
10
738
610
622
75
804
208
226
987
393
227
24
652
916
49
675
407
236
825
140
559
976
467
112
212
814
612
117
898
916
725
218
164
21
425
330
108
747
316
597
278
378
506
319
968
631
683
717
97
873
664
690
331
652
284
698
183
665
17
258
BubbleSort - 0 steps
10
185
221
572
448
849
963
373
806
521
65
709
977
682
161
822
987
148
305
497
10
332
972
450
877
447
65
349
599
932
177
347
174
774
721
145
182
412
402
178
888
724
12
276
968
375
716
565
896
905
179
643
611
247
252
519
542
85
986
401
772
417
132
563
373
166
325
947
321
349
879
472
382
401
631
327
365
438
933
67
484
891
843
805
895
212
466
969
285
909
674
25
533
617
369
207
285
620
544
90
InsertionSort - 0 steps
591
741
521
653
959
608
135
250
241
612
948
823
867
914
752
742
26
620
333
29
899
855
112
384
363
411
186
853
914
905
411
828
350
871
315
324
371
289
743
338
338
919
737
147
488
949
599
404
982
318
314
788
49
751
992
61
788
654
852
465
1000
711
396
12
30
6
956
143
522
233
420
591
84
56
182
852
706
178
808
890
811
215
209
619
888
169
690
97
115
698
512
271
84
879
827
248
575
994
979
214
ShellSort - 0 steps
76
416
739
440
333
243
912
221
354
517
370
968
915
398
461
407
557
678
650
428
820
991
18
266
631
16
388
365
128
391
884
22
630
788
648
354
590
330
856
235
143
850
798
449
703
221
478
303
614
731
386
703
371
360
212
244
460
260
847
711
207
141
286
21
153
655
267
566
390
136
879
259
688
328
222
744
330
511
542
998
102
684
148
451
494
852
219
957
651
378
534
873
37
791
692
922
59
275
758
437
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