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.

993
507
191
177
679
362
654
195
504
252
67
306
868
722
326
135
365
20
304
462
772
923
199
213
65
495
827
455
759
149
516
511
179
611
654
568
372
672
841
763
386
741
69
531
233
159
860
331
899
923
780
156
346
236
596
388
547
584
766
527
629
12
337
125
901
425
979
176
979
31
989
664
505
760
856
344
77
984
761
145
390
838
62
683
939
688
453
847
818
602
957
647
429
650
625
48
673
437
691
446
BubbleSort - 0 steps
700
569
98
613
928
706
76
561
452
943
596
461
588
434
270
940
673
218
303
912
199
791
806
832
207
966
1000
638
809
716
951
514
190
962
486
164
331
381
431
406
119
636
12
498
861
516
837
387
871
909
980
28
163
790
780
961
949
313
455
833
175
324
892
266
956
637
533
997
239
648
130
886
803
326
856
858
706
883
226
174
218
873
107
899
112
354
841
197
582
432
249
89
322
130
256
549
992
712
999
246
InsertionSort - 0 steps
384
416
304
60
218
333
733
303
803
274
534
29
230
726
850
77
348
407
20
192
956
114
500
742
934
958
922
457
68
997
500
220
793
606
979
62
898
958
26
855
450
712
76
46
976
143
417
42
716
661
512
851
600
90
159
178
884
827
901
826
682
333
465
275
841
1000
810
227
909
324
295
773
111
374
984
293
340
509
562
113
71
799
400
581
724
270
908
839
96
811
498
324
313
643
945
914
437
571
714
965
ShellSort - 0 steps
741
682
798
994
952
457
166
988
545
628
416
561
459
521
960
931
215
870
796
415
250
191
295
97
92
162
619
365
585
334
913
807
56
794
211
332
266
196
313
470
588
428
834
751
323
334
40
110
232
499
402
415
808
424
651
460
728
647
774
419
771
364
941
238
830
834
809
978
300
266
428
545
895
6
546
397
102
232
600
713
276
599
806
294
342
758
264
736
102
941
573
439
707
82
161
15
944
74
634
28
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