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.

408
616
456
778
695
476
698
148
53
991
525
947
959
74
690
970
771
274
980
551
897
731
35
193
782
173
792
970
267
523
516
598
405
548
216
768
437
777
171
361
495
925
246
619
456
241
73
205
776
8
154
37
980
815
799
330
105
372
217
509
693
749
150
55
151
787
492
715
858
902
47
96
968
144
807
587
974
654
167
635
827
808
123
275
32
640
594
257
969
462
696
153
177
315
754
901
202
268
980
969
BubbleSort - 0 steps
657
914
716
408
824
556
762
87
889
559
350
546
987
332
968
788
999
447
380
962
877
821
231
215
453
838
701
718
957
252
301
245
110
17
437
683
461
531
662
297
610
463
637
64
497
579
84
986
5
308
161
175
129
545
635
376
157
31
512
386
130
254
552
216
911
594
643
419
200
903
154
522
193
19
74
109
338
402
628
246
912
86
407
367
510
35
401
739
53
752
651
632
968
188
89
269
169
829
924
988
InsertionSort - 0 steps
171
432
801
250
597
813
467
464
789
996
262
699
575
694
671
2
833
455
163
804
813
678
921
568
65
541
428
282
41
315
663
962
266
726
431
986
568
230
344
765
826
446
893
447
149
671
93
627
463
721
282
214
161
670
557
859
12
931
242
173
79
325
19
252
387
548
881
930
322
523
684
61
241
421
622
809
860
578
287
298
443
20
793
710
595
150
886
501
216
138
288
243
584
829
896
940
254
841
52
216
ShellSort - 0 steps
21
364
944
221
651
552
830
536
603
730
426
915
825
538
962
278
522
449
767
306
315
357
658
111
586
2
994
152
92
934
678
66
670
937
759
932
879
497
554
262
376
414
716
216
619
256
289
265
661
966
604
526
516
13
241
450
726
868
151
184
114
558
391
186
270
697
86
763
715
959
623
255
692
321
588
992
379
372
669
892
667
141
709
118
34
438
908
40
911
386
733
741
953
41
505
133
773
250
778
792
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