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.

664
298
143
147
364
268
198
186
14
722
758
811
320
540
288
636
789
197
72
164
102
69
334
151
419
309
679
536
627
790
750
372
174
848
502
336
958
57
874
993
679
910
137
105
710
923
625
591
283
11
773
790
143
532
126
498
122
78
790
364
576
844
191
917
392
503
643
44
168
187
704
244
677
357
877
679
577
353
197
282
239
88
184
477
48
611
189
28
941
19
201
199
283
499
204
51
565
974
224
196
BubbleSort - 0 steps
242
524
577
721
609
330
780
756
827
844
297
243
392
147
322
817
908
504
657
369
644
794
96
732
73
347
444
977
964
274
315
983
958
798
566
877
915
347
67
356
943
383
647
237
858
648
221
381
160
189
663
237
920
944
469
860
327
736
387
199
834
147
344
247
226
456
99
578
594
623
240
392
691
331
807
408
882
689
983
67
821
790
813
981
250
226
828
103
51
900
803
324
977
184
556
359
150
677
483
175
InsertionSort - 0 steps
160
997
755
860
544
927
546
685
218
255
981
134
909
931
670
971
971
414
219
137
528
150
907
360
978
636
714
69
524
462
417
718
279
787
2
990
399
598
953
675
698
977
237
812
393
148
234
307
738
24
920
852
349
411
827
82
712
248
946
656
870
1000
977
29
860
350
7
875
66
45
197
401
147
755
647
9
564
856
286
902
299
773
125
311
269
53
361
985
504
322
431
230
348
545
895
718
520
852
306
963
ShellSort - 0 steps
361
612
810
92
894
271
863
760
110
878
75
900
963
258
903
16
464
748
52
806
687
734
876
277
468
897
289
680
225
91
540
470
333
210
860
601
135
24
879
710
548
986
44
750
852
987
569
485
323
350
326
817
998
605
614
196
751
142
7
904
933
46
531
748
705
924
893
846
854
1000
846
822
349
933
35
71
438
978
843
745
948
739
846
916
108
566
341
780
447
604
713
325
790
480
562
585
456
582
188
761
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