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.

338
769
145
526
242
436
184
770
235
418
488
241
820
667
137
803
504
73
220
119
977
36
748
365
72
262
344
783
51
350
887
634
102
965
584
795
211
836
353
939
997
927
438
435
850
350
28
352
543
812
123
1000
598
518
663
239
340
384
746
543
918
384
734
547
724
580
953
973
415
250
520
668
977
565
616
748
892
628
555
29
635
842
776
395
559
967
229
515
829
23
808
808
166
921
604
362
90
589
384
639
BubbleSort - 0 steps
714
349
597
831
659
915
234
398
98
165
730
339
220
298
918
242
269
471
702
101
956
748
357
658
869
507
569
448
268
845
314
168
850
245
840
365
220
862
426
27
556
665
209
532
983
683
713
249
573
722
242
860
93
49
115
289
98
36
67
335
773
131
890
368
212
565
818
896
470
564
556
462
469
895
884
410
106
735
629
986
737
441
478
168
724
572
337
367
246
773
6
958
943
380
769
168
916
820
669
371
InsertionSort - 0 steps
106
998
749
235
279
72
166
642
352
261
375
218
970
938
625
543
590
483
670
997
215
786
527
532
871
31
946
856
240
96
331
967
661
378
569
340
409
761
412
825
166
819
8
209
748
743
205
175
619
811
20
305
585
816
975
350
123
854
462
795
729
668
376
72
173
802
523
756
9
377
952
880
376
960
762
341
593
400
584
566
762
154
97
743
484
184
296
139
528
72
408
15
82
112
907
887
123
956
701
989
ShellSort - 0 steps
802
447
757
891
35
625
926
696
237
623
994
67
194
180
777
83
520
828
951
167
36
940
227
534
146
351
601
546
67
642
130
262
585
549
10
534
328
313
791
691
724
696
103
322
133
729
809
966
192
220
421
787
795
343
426
797
661
695
162
664
4
423
162
801
14
445
348
501
729
954
412
132
30
431
543
928
889
947
862
627
680
198
930
54
16
413
716
998
328
478
393
977
379
332
320
506
886
427
513
137
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