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.

957
368
803
763
248
293
541
338
743
510
822
970
384
262
737
91
973
306
385
185
233
507
183
412
792
715
836
370
66
996
589
96
726
231
35
246
598
704
37
550
803
114
157
351
574
713
100
190
919
108
728
306
995
211
506
943
632
169
236
397
964
532
462
160
262
89
442
383
102
330
316
187
60
398
953
841
325
242
762
327
157
458
816
400
533
191
768
666
880
450
259
818
583
861
992
46
357
773
574
70
BubbleSort - 0 steps
564
86
363
203
765
952
273
984
575
901
34
966
853
729
267
472
426
928
237
840
580
560
727
264
306
858
277
500
140
307
98
529
129
440
810
837
999
957
898
730
884
549
304
544
879
772
357
442
641
25
145
562
486
159
522
442
545
53
698
52
895
119
744
796
869
417
401
234
313
123
726
510
939
689
314
873
666
108
340
747
737
936
97
325
200
946
168
820
623
37
977
706
7
575
984
299
551
874
570
849
InsertionSort - 0 steps
77
58
154
543
952
662
523
436
945
577
46
151
62
458
666
375
846
578
755
502
281
244
663
453
70
887
159
583
397
166
307
513
786
952
247
111
684
798
139
980
204
780
713
289
817
545
409
100
350
260
571
434
813
472
343
231
32
812
269
235
582
84
104
795
562
300
56
672
238
272
352
388
278
185
609
715
739
557
915
266
545
141
349
194
400
633
555
424
30
302
293
460
875
882
687
457
255
801
180
337
ShellSort - 0 steps
714
892
345
645
209
31
465
847
990
810
832
111
730
347
614
152
561
587
467
602
99
364
676
768
560
953
492
602
622
727
275
390
546
622
92
633
7
227
694
698
226
147
625
912
1
482
399
469
266
884
569
696
16
391
208
695
649
703
774
971
485
255
345
952
25
648
939
847
377
814
809
367
318
654
900
67
48
237
640
453
746
197
558
843
623
545
454
492
113
867
284
928
450
555
355
240
524
556
797
920
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