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.

187
132
994
923
39
766
261
396
653
54
268
551
237
648
113
78
544
576
822
467
755
344
9
883
505
369
523
495
369
873
901
231
649
957
799
307
464
336
892
874
189
814
668
815
971
808
775
449
897
440
275
473
190
552
727
101
571
807
558
310
231
181
704
724
979
196
519
216
289
829
411
464
155
86
708
634
674
905
294
668
824
240
692
786
381
918
391
473
688
365
350
980
859
31
807
502
362
432
979
495
BubbleSort - 0 steps
681
457
944
101
163
390
420
381
755
843
736
80
904
168
887
643
692
764
8
935
777
335
276
957
493
104
683
250
880
520
594
646
578
53
299
575
767
82
414
489
451
534
544
694
914
401
530
686
83
672
195
970
853
495
61
125
861
615
514
929
672
331
354
223
243
632
905
43
814
695
786
813
569
189
909
77
32
624
674
107
239
390
387
193
274
380
520
687
205
282
333
734
592
536
193
158
84
61
322
102
InsertionSort - 0 steps
539
828
960
336
487
801
745
989
755
759
727
931
5
86
939
696
389
649
165
607
341
252
637
323
861
922
221
653
467
632
698
109
43
677
405
270
808
23
431
493
254
82
174
312
28
487
141
949
831
429
476
156
310
370
173
551
886
98
437
88
151
993
422
209
330
603
555
347
846
490
816
752
858
510
973
603
661
138
250
902
99
897
815
854
670
134
151
445
929
43
856
846
342
943
374
216
173
740
848
217
ShellSort - 0 steps
768
250
251
251
303
642
2
998
747
569
324
406
470
426
112
608
184
617
785
434
126
709
192
229
489
773
881
448
540
108
982
536
725
235
102
368
236
233
721
703
971
528
174
906
778
909
452
710
232
160
116
982
487
736
562
376
603
989
223
204
317
515
693
969
266
755
267
453
282
731
905
580
849
41
171
298
506
664
102
321
75
656
640
519
978
38
166
115
432
496
831
323
617
978
938
490
680
591
932
838
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