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.

693
40
450
615
127
911
688
525
991
239
122
393
292
610
469
916
451
204
867
26
932
827
6
295
191
608
381
929
196
137
224
824
6
613
633
838
132
641
461
522
535
671
634
945
387
116
483
683
917
333
739
897
634
91
63
83
406
996
233
435
134
352
566
727
692
604
989
952
952
758
420
895
909
245
994
443
221
686
922
641
472
51
658
798
38
592
64
602
717
531
518
724
997
753
296
728
475
269
722
24
BubbleSort - 0 steps
29
377
917
677
746
68
405
158
568
726
406
717
1
996
265
116
48
732
81
793
702
513
405
490
282
460
14
21
697
248
788
336
301
450
309
996
187
935
569
312
29
278
704
185
852
127
111
197
255
309
897
709
55
479
327
115
470
84
576
687
333
387
521
378
275
314
224
392
959
505
343
275
981
940
675
588
911
430
466
403
397
837
31
108
160
787
266
308
238
107
550
548
346
964
909
695
698
459
273
781
InsertionSort - 0 steps
444
712
173
16
28
296
463
547
225
461
246
516
134
552
889
682
494
958
137
847
826
447
531
146
669
774
95
360
605
262
297
671
202
788
241
482
548
43
981
806
127
458
944
885
776
188
423
211
120
411
184
550
607
151
466
708
141
202
936
321
569
580
114
930
139
332
849
735
692
102
918
612
330
775
830
509
82
793
758
254
225
150
391
196
478
766
820
419
171
745
967
710
344
198
290
76
83
394
849
958
ShellSort - 0 steps
606
917
994
165
565
557
284
806
789
793
515
656
572
971
827
669
291
510
942
644
327
975
652
846
115
246
824
531
224
756
523
450
277
165
550
916
927
642
403
964
744
457
283
748
804
875
452
363
549
463
458
335
37
171
513
487
696
617
375
738
195
21
395
786
535
177
247
983
502
649
973
736
444
939
582
537
441
312
119
291
354
538
4
950
905
815
927
73
29
93
478
724
982
688
935
819
594
31
139
805
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