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.

245
242
769
982
624
484
49
808
683
793
803
946
521
792
400
40
908
249
849
830
306
202
35
283
852
298
954
89
95
135
388
843
400
915
3
358
699
925
409
205
47
544
606
464
948
973
314
326
680
621
849
564
202
129
448
692
813
560
852
731
8
730
786
916
181
485
583
589
895
60
31
808
990
251
495
52
637
49
516
42
681
20
448
73
766
862
385
583
5
163
45
58
244
231
606
804
803
358
987
492
BubbleSort - 0 steps
16
352
774
662
781
339
873
253
985
707
782
418
393
990
561
621
446
364
573
309
804
529
869
144
893
514
341
339
982
245
676
179
950
806
277
495
824
724
537
757
939
888
861
430
437
825
722
444
632
406
131
449
17
756
550
774
451
954
704
135
586
851
45
980
182
93
288
536
372
674
361
34
758
328
309
292
70
843
901
308
488
436
467
329
784
310
158
822
7
613
697
799
768
85
667
203
880
507
605
468
InsertionSort - 0 steps
450
530
878
667
628
359
392
776
678
47
490
927
709
215
463
656
697
294
204
655
208
757
973
953
616
722
724
409
281
703
84
637
787
819
581
903
900
947
481
172
122
69
148
503
41
123
852
662
682
50
342
637
796
852
263
152
695
783
771
364
3
208
429
133
226
430
789
838
397
522
467
316
963
910
357
685
921
271
932
583
276
296
302
891
810
164
804
669
511
384
177
584
656
452
853
849
133
910
762
115
ShellSort - 0 steps
453
688
431
946
779
631
691
638
615
977
963
122
794
7
845
700
757
878
849
363
65
485
930
436
325
148
896
116
360
872
658
572
541
882
476
455
807
816
568
436
621
632
636
587
280
466
61
245
485
104
561
639
989
658
663
667
362
790
648
258
10
509
417
235
940
145
992
709
825
179
600
501
160
721
539
351
20
760
799
504
950
418
115
355
588
771
771
770
589
724
294
84
38
293
150
964
857
695
755
820
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