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.

924
538
184
612
467
966
586
751
443
511
811
704
159
587
155
173
206
903
476
104
213
407
704
137
731
487
331
525
524
649
359
895
195
455
586
323
436
848
616
543
120
732
4
529
927
64
306
224
220
826
418
724
921
104
144
618
166
432
336
51
321
581
505
224
981
19
230
942
708
654
511
696
251
625
486
472
310
882
452
113
341
882
137
865
313
422
479
144
224
865
78
219
861
70
488
27
774
443
827
309
BubbleSort - 0 steps
18
679
20
127
374
506
347
318
967
769
676
952
518
882
851
181
512
465
625
455
972
212
162
700
333
76
941
311
287
992
77
982
648
970
250
826
937
708
298
455
88
717
115
790
772
280
864
905
19
994
40
544
965
350
426
53
154
651
421
777
328
396
247
446
955
428
767
530
311
387
404
266
357
134
579
399
581
8
625
528
531
811
845
460
823
409
317
37
833
478
393
667
133
659
850
538
988
155
2
39
InsertionSort - 0 steps
113
206
99
685
105
512
973
635
291
992
320
741
383
687
717
719
466
868
142
624
961
538
417
469
161
664
801
151
203
555
4
45
347
311
514
381
913
142
897
503
605
164
625
565
37
590
702
545
197
766
882
612
436
603
85
386
276
573
541
798
275
997
119
83
663
139
552
236
470
160
11
235
178
933
620
967
832
92
525
459
119
303
505
159
774
899
821
636
627
525
86
326
16
639
880
254
157
583
514
592
ShellSort - 0 steps
703
254
991
288
353
376
739
715
344
164
111
224
998
529
857
814
632
640
144
526
87
920
885
556
218
365
252
542
293
877
418
631
197
688
458
845
905
530
969
904
273
634
994
186
794
91
245
526
582
904
114
832
257
228
395
468
407
822
862
9
831
523
639
358
736
662
62
712
371
500
985
575
238
44
82
242
310
305
43
974
348
50
923
222
567
177
318
687
591
28
965
22
170
71
148
147
569
803
302
663
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