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.

290
796
568
845
460
924
436
935
286
644
812
63
456
204
524
320
766
492
821
141
302
917
257
25
48
474
224
397
324
167
402
998
232
974
428
418
112
453
418
702
4
299
777
84
180
43
681
520
637
71
879
363
334
78
726
235
80
177
712
963
327
962
55
642
360
317
650
865
21
836
328
693
461
523
964
270
143
304
941
545
669
832
921
131
718
497
766
898
130
468
607
23
204
935
970
358
376
418
998
159
BubbleSort - 0 steps
352
91
127
990
705
104
238
395
468
694
316
455
991
564
139
959
948
439
157
380
581
879
648
685
394
101
296
529
933
896
891
824
501
463
54
123
989
683
766
660
393
479
43
712
586
960
274
88
906
662
336
385
153
281
734
114
242
690
548
488
841
677
450
958
488
981
227
544
199
935
434
350
956
452
596
442
342
19
569
318
709
619
222
448
437
769
352
445
254
631
316
641
650
387
414
388
105
471
831
336
InsertionSort - 0 steps
672
213
521
220
309
350
123
742
373
537
112
705
795
372
93
180
496
374
564
774
203
885
605
708
123
598
824
190
350
355
357
802
164
558
366
831
300
983
418
980
360
997
158
713
99
124
201
374
671
342
764
353
950
630
924
856
426
27
13
353
517
143
737
658
214
324
334
373
874
687
388
327
609
42
515
154
887
136
357
474
182
590
265
831
264
496
487
70
943
539
717
243
102
361
105
742
773
691
822
650
ShellSort - 0 steps
479
959
595
291
741
781
857
575
872
729
239
80
554
82
291
36
934
16
186
824
364
690
543
228
606
66
500
271
519
218
615
547
454
863
528
58
325
814
546
465
688
149
691
64
891
671
944
744
942
794
97
983
262
605
779
258
672
480
368
307
280
941
548
151
598
14
440
32
138
325
160
903
266
677
807
435
406
387
922
325
759
575
858
39
874
680
971
438
353
252
568
21
561
666
852
432
289
329
756
842
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