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.

832
363
610
37
33
1
636
204
213
648
861
554
36
164
355
454
264
275
209
154
750
585
646
549
525
480
235
38
625
743
467
265
84
364
970
408
786
641
836
658
661
701
950
59
717
945
189
594
969
104
223
665
700
89
540
265
332
189
403
150
904
823
34
508
612
610
667
523
875
168
123
311
784
216
162
364
704
160
924
640
75
486
388
211
265
131
110
569
624
745
737
186
111
451
664
676
23
337
824
347
BubbleSort - 0 steps
231
430
73
836
556
904
596
124
463
726
160
937
704
421
507
551
995
34
754
192
494
782
907
16
969
382
739
447
837
446
344
196
167
55
851
685
273
236
491
474
87
463
804
201
95
829
209
46
488
373
966
251
826
562
404
117
477
403
565
419
403
375
965
494
573
269
434
983
204
92
835
858
16
147
598
735
625
749
720
34
708
937
663
647
185
304
423
309
482
964
640
374
33
663
138
102
649
944
500
595
InsertionSort - 0 steps
434
846
671
132
715
957
840
958
353
107
299
305
536
416
15
487
505
582
380
387
941
485
755
80
786
185
90
150
412
803
561
724
227
452
431
613
201
327
68
947
223
586
883
626
373
451
208
693
895
803
101
972
338
923
828
272
444
826
927
94
380
104
867
688
397
597
260
241
896
683
168
736
256
576
417
948
371
262
556
148
893
188
544
753
489
780
671
987
527
386
587
29
371
373
280
919
514
625
686
651
ShellSort - 0 steps
375
685
73
82
714
429
192
221
293
36
874
297
590
942
437
120
904
620
218
462
600
183
383
833
43
426
607
397
196
12
908
438
454
433
162
514
302
837
977
120
22
225
325
74
554
477
589
189
498
696
274
892
536
433
766
583
768
933
247
970
200
563
147
640
198
488
979
949
781
141
925
329
629
91
261
490
377
182
324
179
968
838
111
697
181
27
213
611
223
144
334
429
76
116
474
55
714
658
806
900
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