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.

562
821
745
780
556
372
17
576
413
792
60
857
247
482
687
855
803
890
636
934
129
872
89
358
762
474
835
659
444
475
18
991
811
720
489
979
490
335
98
819
229
398
34
13
825
5
244
506
368
559
885
960
805
499
595
288
899
388
370
90
178
250
237
141
206
76
616
452
77
569
239
513
514
757
653
58
738
683
945
284
376
350
505
834
145
384
223
783
346
573
497
136
527
430
597
80
333
599
45
464
BubbleSort - 0 steps
433
956
585
972
18
848
738
512
914
354
202
79
523
24
544
689
250
693
444
478
274
6
164
396
251
434
794
894
174
395
448
125
603
52
147
952
554
687
121
99
21
295
621
197
959
139
121
694
371
552
967
3
676
839
447
1000
42
742
963
617
336
50
828
885
421
492
371
873
478
414
857
535
606
709
777
175
615
650
511
207
405
931
177
602
599
182
420
479
893
696
793
682
305
524
353
793
100
723
959
623
InsertionSort - 0 steps
202
617
351
56
922
219
459
313
167
706
868
676
87
854
236
663
998
465
638
544
270
908
792
892
736
925
954
256
461
451
112
870
255
1
366
863
963
790
955
635
486
238
77
716
223
805
186
402
474
953
776
56
747
151
863
86
900
935
266
584
924
767
941
790
490
111
705
547
723
71
656
382
645
835
354
553
1
911
990
578
114
85
621
184
430
418
986
163
216
23
507
691
417
546
28
682
835
595
861
298
ShellSort - 0 steps
878
156
611
997
707
787
167
817
54
620
512
482
487
931
228
97
611
87
260
44
266
713
438
735
425
150
66
21
390
514
745
986
976
171
943
192
998
335
96
488
384
803
843
509
264
936
612
111
572
268
243
451
244
551
592
431
149
420
850
7
859
534
707
560
571
242
506
966
734
784
944
62
589
257
245
195
523
433
770
3
55
133
444
168
18
456
910
528
245
857
819
715
48
26
146
245
234
523
814
330
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