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.

844
272
316
520
765
546
253
357
329
262
505
144
981
913
125
465
97
209
332
62
941
722
857
224
854
358
981
352
438
786
438
256
766
444
970
584
101
283
301
146
40
8
758
622
863
281
135
481
859
295
766
126
775
729
290
705
810
819
850
411
579
936
695
490
295
971
13
470
398
187
925
351
408
557
67
722
372
845
366
213
303
630
53
613
970
673
815
244
911
101
491
270
302
655
533
80
904
829
424
229
BubbleSort - 0 steps
245
495
536
365
412
657
583
948
267
840
160
531
746
371
143
465
282
156
939
309
987
654
374
636
806
941
903
95
457
408
38
567
173
962
934
219
144
535
554
25
675
495
79
871
731
210
535
280
333
459
912
882
322
954
700
949
19
138
375
890
318
193
379
620
387
640
809
909
476
92
772
994
594
349
724
291
983
51
268
442
560
129
163
161
908
383
73
550
172
540
534
831
604
103
52
758
827
842
183
373
InsertionSort - 0 steps
850
616
713
896
960
921
323
321
532
563
651
205
888
100
962
348
802
367
157
460
3
685
447
682
874
847
969
612
144
844
527
229
511
664
516
414
112
124
946
466
58
653
455
366
860
653
921
729
90
230
301
576
192
941
163
468
5
742
175
372
290
992
35
22
265
19
952
353
523
638
360
410
759
552
418
190
333
786
929
869
841
788
726
367
552
281
103
975
297
71
269
710
584
854
456
994
831
596
496
860
ShellSort - 0 steps
129
114
534
512
755
245
190
4
841
159
66
533
34
444
955
645
618
12
494
923
673
437
886
486
178
893
119
891
246
783
981
243
989
456
300
891
686
386
757
504
452
82
844
954
348
524
76
37
930
494
468
995
527
165
637
869
369
858
678
369
798
979
15
231
943
13
976
331
363
517
45
454
321
549
482
158
994
77
164
846
859
898
969
898
177
858
58
406
24
185
118
7
181
239
140
591
306
67
853
174
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