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.

630
191
237
913
514
793
32
292
424
642
611
48
868
731
338
858
267
752
628
473
384
483
485
225
110
683
899
332
566
689
144
552
217
68
864
788
587
706
448
174
566
107
451
971
440
816
14
573
191
459
4
29
930
495
414
798
85
584
950
785
538
981
82
22
890
537
631
116
130
320
139
708
617
490
655
4
296
185
619
303
511
215
177
260
894
573
601
541
324
841
790
665
899
331
695
891
437
210
956
489
BubbleSort - 0 steps
535
743
260
216
719
364
247
792
982
336
780
103
717
140
166
515
860
359
935
637
284
946
543
91
492
734
529
304
505
926
605
738
276
755
301
250
369
784
328
464
730
428
413
694
702
21
153
278
641
999
420
518
331
215
367
944
716
428
134
310
515
263
866
244
871
268
964
106
149
32
511
973
273
261
584
463
430
307
654
519
501
611
688
474
310
893
914
651
407
333
477
779
722
59
121
927
149
915
798
651
InsertionSort - 0 steps
842
410
76
772
675
209
281
970
781
324
527
177
389
796
634
820
549
404
575
240
54
377
124
408
951
498
341
154
849
552
493
758
518
301
914
540
156
826
668
651
671
963
432
922
173
547
367
128
896
518
930
63
66
801
581
288
827
940
454
241
325
474
954
429
183
125
882
837
699
124
38
597
954
335
825
691
333
651
12
140
31
309
307
255
67
585
412
914
114
609
163
184
574
683
919
677
723
182
112
880
ShellSort - 0 steps
974
730
262
176
314
195
762
893
600
91
247
126
122
662
749
331
713
690
485
866
304
451
352
172
228
57
711
104
821
428
739
736
552
229
676
786
988
650
20
688
861
395
996
796
661
492
443
482
522
222
815
527
896
857
678
453
963
833
647
400
385
12
745
167
605
766
592
294
313
10
876
788
195
383
60
123
861
844
232
296
145
853
811
997
969
64
483
757
533
40
131
202
742
995
77
954
454
979
584
214
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