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.

341
765
551
925
197
918
467
656
103
4
394
137
138
934
111
511
701
198
469
630
257
329
471
707
652
248
294
508
324
736
463
755
60
117
713
737
429
193
434
711
675
706
472
901
771
82
116
780
380
583
635
836
607
281
675
360
943
302
742
366
365
732
580
675
244
252
673
114
768
581
903
144
858
8
973
650
960
993
328
810
60
647
621
152
366
158
954
1000
207
740
267
344
924
581
946
990
241
347
616
96
BubbleSort - 0 steps
962
854
461
208
66
181
293
767
771
273
372
268
267
316
165
696
611
774
61
641
710
230
30
413
794
47
225
185
602
717
208
713
356
943
114
657
695
715
270
63
720
916
489
898
42
353
966
10
326
55
330
367
90
169
354
955
146
259
963
67
98
668
202
547
903
254
151
325
991
501
469
652
371
264
910
754
541
267
763
875
551
905
384
592
37
26
390
149
341
733
4
434
620
545
493
567
164
823
900
27
InsertionSort - 0 steps
729
102
684
172
955
876
513
582
438
165
235
809
734
214
477
705
8
221
241
535
463
798
884
515
191
342
9
264
990
241
22
550
486
727
414
814
731
19
32
481
683
59
326
393
38
373
285
370
447
609
770
702
816
486
481
915
147
904
750
810
978
323
513
588
442
233
208
930
735
405
832
549
705
425
837
642
688
96
691
556
74
450
887
544
879
42
488
15
64
978
469
948
201
869
18
844
664
131
849
789
ShellSort - 0 steps
532
49
145
778
375
495
834
786
415
949
967
363
446
353
233
412
577
545
302
359
24
689
829
908
841
314
657
114
71
136
273
61
866
910
294
588
515
735
279
329
423
551
884
190
864
801
343
363
621
618
469
88
2
763
373
126
42
315
913
625
52
824
564
765
411
738
668
456
192
224
340
339
469
744
453
535
918
230
857
796
947
967
797
273
162
381
485
167
8
916
752
582
938
280
3
767
125
729
26
670
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