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.

808
74
915
660
983
412
642
278
575
677
369
790
74
681
841
592
659
339
834
178
969
152
557
937
629
781
792
347
649
732
926
385
711
57
300
268
990
962
597
80
417
194
75
331
136
911
252
525
841
682
652
851
392
820
991
817
863
839
806
603
86
203
261
182
971
957
584
168
588
634
204
199
326
933
156
241
967
592
65
997
807
245
978
338
166
728
570
905
31
562
490
187
613
293
595
318
840
621
468
640
BubbleSort - 0 steps
170
272
910
166
337
342
685
246
781
798
872
668
178
371
533
45
621
386
955
3
173
429
575
772
896
652
683
810
330
780
393
236
966
720
75
826
854
649
464
198
887
563
598
450
561
556
748
43
834
372
741
849
706
436
390
633
261
531
823
578
36
719
650
71
898
778
875
513
648
891
159
357
709
217
617
788
985
681
722
987
688
783
713
530
593
201
600
838
141
691
188
854
654
960
799
18
352
901
237
609
InsertionSort - 0 steps
843
664
816
807
948
230
526
359
942
847
622
711
608
948
564
513
901
830
272
261
328
948
844
459
524
9
231
450
672
581
424
421
807
501
184
148
97
865
533
697
812
336
737
280
874
51
149
763
603
466
188
32
753
801
217
785
475
837
428
224
958
607
498
216
964
553
48
645
508
376
273
121
46
671
419
108
341
635
168
574
875
918
662
874
323
270
324
184
641
130
542
970
262
536
256
189
460
344
448
521
ShellSort - 0 steps
386
626
143
407
370
26
491
727
242
216
788
527
98
201
450
722
269
86
122
141
581
113
376
180
798
104
169
998
374
539
93
141
31
888
390
255
926
899
80
162
761
522
758
336
654
322
543
572
545
448
427
387
849
815
115
752
796
869
597
39
480
877
367
544
77
714
242
908
98
892
852
725
639
938
798
270
712
775
250
320
333
10
693
995
306
567
851
410
493
325
200
516
213
701
584
584
739
412
346
507
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