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.

624
732
447
539
941
3
948
881
596
712
40
451
632
469
948
343
226
82
494
430
643
286
595
480
841
814
314
460
570
101
463
955
498
748
733
73
606
351
647
45
928
287
223
240
522
351
287
633
45
303
587
419
388
415
60
869
109
610
953
245
265
968
717
244
671
454
809
286
155
825
324
383
2
622
43
384
396
352
980
912
648
260
698
702
738
50
490
763
694
597
558
561
815
119
422
157
353
602
969
995
BubbleSort - 0 steps
296
86
88
905
916
637
838
38
615
229
41
528
201
559
645
460
119
335
882
5
203
654
749
118
795
508
495
840
172
15
148
116
145
164
291
920
42
350
609
87
826
89
292
881
146
306
199
774
960
710
265
411
318
516
220
310
827
755
914
740
349
738
94
241
788
401
577
555
936
5
955
776
580
75
10
558
628
463
344
366
699
51
691
410
364
727
243
797
293
217
312
882
71
234
388
970
230
568
323
49
InsertionSort - 0 steps
536
703
460
237
464
886
122
466
405
285
807
518
242
241
394
799
822
14
665
311
991
712
225
544
809
627
734
374
707
224
262
325
513
700
225
866
323
517
994
538
15
320
77
109
734
526
24
385
477
180
402
109
398
331
142
188
888
900
435
719
879
802
45
719
918
123
990
799
518
826
457
997
658
542
912
798
339
954
646
807
282
876
559
136
490
813
475
224
282
523
210
687
488
891
236
365
410
250
946
468
ShellSort - 0 steps
893
547
689
429
193
612
95
688
56
136
211
618
322
68
932
64
249
316
499
626
670
87
205
458
107
709
33
572
737
384
213
663
35
125
294
585
874
896
64
134
274
249
550
905
255
204
140
273
368
802
45
776
287
461
270
922
100
713
647
922
712
521
807
49
105
209
47
429
960
954
683
495
243
490
640
265
652
628
240
856
536
319
996
487
601
793
15
169
498
760
69
635
965
907
747
780
863
472
357
112
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