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.

661
695
48
713
982
598
830
913
246
94
395
878
957
985
485
796
590
987
695
4
514
509
629
217
284
397
837
740
172
430
853
469
111
223
134
967
625
66
184
671
631
225
91
340
646
746
745
651
320
904
681
238
103
507
453
264
603
837
577
786
516
442
459
56
107
605
272
878
182
393
308
332
432
942
582
338
461
147
189
743
646
388
445
540
443
589
181
604
17
443
572
717
996
868
764
326
657
522
483
889
BubbleSort - 0 steps
831
761
59
904
587
394
390
992
132
335
315
512
524
419
355
280
723
50
410
580
717
126
895
341
884
393
611
910
700
919
847
467
443
953
984
755
31
201
294
485
710
381
207
392
519
593
29
422
28
15
720
282
186
735
728
731
941
446
720
931
16
798
302
366
871
814
304
287
211
172
166
286
954
789
147
307
751
157
425
517
930
505
123
865
815
124
173
866
668
205
679
822
475
436
741
572
830
598
963
754
InsertionSort - 0 steps
843
769
685
96
608
854
562
761
755
606
237
572
530
810
510
507
912
926
949
490
926
889
649
516
469
67
771
766
916
489
536
530
257
733
165
177
655
881
901
755
725
429
634
311
168
383
923
596
510
659
205
633
996
365
992
955
609
786
994
809
841
512
670
637
18
91
962
695
968
469
471
860
466
134
291
180
775
413
551
478
42
873
855
618
548
512
80
706
301
988
303
671
593
613
74
862
32
892
454
135
ShellSort - 0 steps
427
735
323
373
468
230
882
487
186
716
128
334
12
907
544
523
473
795
352
56
607
444
930
218
229
63
578
558
538
845
489
917
681
432
924
68
385
705
561
548
461
204
13
313
712
98
373
81
909
564
455
940
17
164
278
362
604
772
149
307
727
716
604
907
923
417
180
954
587
410
401
524
565
530
217
990
778
991
463
901
75
463
29
249
703
786
124
335
169
447
628
980
286
164
579
144
474
741
284
383
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