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.

412
502
640
120
103
354
927
153
136
317
975
27
860
126
964
213
454
740
339
808
249
576
446
403
25
272
309
842
854
109
907
266
458
986
285
551
364
383
769
801
130
795
937
947
590
984
355
398
467
130
798
217
169
2
690
496
728
218
378
457
157
395
968
451
721
203
695
820
247
578
440
413
179
943
627
458
282
884
651
629
661
934
996
136
104
689
608
750
913
620
119
481
654
761
685
556
948
743
936
697
BubbleSort - 0 steps
455
746
417
803
545
655
387
666
938
616
475
330
636
763
434
536
574
973
137
944
416
375
729
868
671
778
992
423
574
863
914
672
652
334
914
485
536
273
609
325
521
174
859
722
362
9
277
688
703
799
664
998
461
711
885
673
65
109
667
36
558
663
664
250
288
128
971
287
934
668
421
698
905
127
646
181
195
517
991
944
443
923
718
956
32
35
220
239
246
605
384
66
384
626
864
300
112
968
423
978
InsertionSort - 0 steps
403
448
595
248
300
426
870
303
695
599
265
162
558
609
581
497
643
216
671
782
463
56
1000
892
355
113
421
727
626
584
36
902
598
211
906
378
525
646
454
439
169
391
230
85
609
557
98
429
677
776
105
113
21
899
306
144
261
633
642
439
264
752
98
628
60
508
636
636
761
625
394
317
204
692
900
926
534
160
138
105
49
531
693
9
284
873
679
200
780
295
262
56
241
717
277
494
309
539
424
797
ShellSort - 0 steps
216
537
458
946
716
495
355
744
261
162
284
731
98
527
509
439
987
503
568
483
674
579
408
416
483
500
128
445
35
610
937
844
800
774
741
614
671
394
878
169
337
863
604
382
398
697
161
366
303
640
835
144
165
655
495
699
462
57
657
766
366
994
307
354
419
775
438
684
436
106
893
49
156
327
476
621
278
720
837
56
947
127
772
586
772
435
27
971
754
78
645
327
845
506
401
288
350
65
497
888
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