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.

503
881
959
715
544
664
81
518
310
529
871
778
344
46
436
770
85
622
804
154
935
2
426
268
872
487
167
291
278
746
178
326
780
322
950
722
960
887
547
292
287
816
915
529
543
36
123
629
282
668
276
389
496
453
798
322
954
554
88
615
886
778
682
180
183
343
911
606
644
37
42
430
57
994
50
648
709
532
322
747
48
242
5
605
632
342
360
857
962
410
386
962
370
767
856
478
678
720
158
472
BubbleSort - 0 steps
978
723
634
166
566
85
480
12
246
219
23
890
817
567
533
324
418
248
754
473
626
911
527
867
820
750
144
35
370
1
870
530
138
809
947
424
552
801
192
585
891
839
249
373
422
291
692
746
540
88
150
212
265
224
667
663
179
578
576
209
689
195
992
754
172
487
666
872
232
643
480
490
349
110
259
731
329
299
235
11
768
680
1000
409
346
143
882
885
307
586
537
23
397
361
996
689
540
81
815
504
InsertionSort - 0 steps
690
898
997
951
347
268
786
448
749
943
811
261
618
89
134
297
679
557
141
195
553
51
465
969
190
847
69
732
171
687
366
854
352
983
450
979
615
854
834
712
184
306
205
273
178
687
953
451
698
801
567
481
313
37
398
651
627
931
326
314
932
972
476
819
287
32
801
920
225
477
411
558
234
701
748
270
292
89
539
247
534
54
855
480
368
214
772
96
181
522
199
570
853
69
1000
131
993
786
545
426
ShellSort - 0 steps
829
67
169
931
427
821
131
784
253
811
878
861
13
498
871
820
384
735
5
660
937
250
589
122
756
931
536
718
658
298
440
515
370
437
548
145
299
694
989
290
900
763
809
615
263
775
517
78
891
588
423
354
106
62
171
468
376
133
884
734
829
655
373
3
700
545
271
97
646
530
543
273
246
958
741
207
376
685
404
208
967
171
303
542
92
153
742
511
413
300
739
610
832
893
235
518
658
374
503
621
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