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.

785
423
190
449
549
946
176
535
543
755
626
137
763
27
274
144
464
385
600
943
810
200
733
777
740
114
23
935
522
884
880
910
152
988
418
962
601
617
41
190
553
584
781
326
545
440
147
106
270
693
690
297
78
649
720
692
766
231
624
450
676
187
896
212
568
2
49
414
228
140
416
792
947
533
110
582
227
53
423
836
432
176
229
835
125
469
68
835
509
954
668
459
861
145
421
954
836
928
386
204
BubbleSort - 0 steps
578
473
201
588
331
72
663
738
574
589
157
291
573
224
753
592
723
343
739
313
552
536
29
324
696
977
425
704
97
787
978
333
884
642
386
408
882
911
157
337
145
168
182
29
970
955
334
772
756
574
798
669
294
763
915
363
598
124
35
103
95
437
319
794
204
38
669
834
483
781
803
433
889
445
728
443
952
301
79
852
635
393
601
887
856
105
220
531
652
980
174
192
946
617
600
836
797
785
446
32
InsertionSort - 0 steps
459
82
328
871
478
561
612
982
69
518
44
670
810
170
891
731
294
949
692
396
327
414
723
691
315
411
608
589
688
969
10
809
620
578
74
126
547
626
487
150
615
689
707
379
210
678
584
708
882
145
216
891
238
657
37
845
965
425
275
936
11
68
591
564
354
901
748
888
922
872
608
502
244
169
487
712
325
993
852
589
246
79
778
324
772
259
919
188
680
620
260
991
153
211
991
205
253
100
320
856
ShellSort - 0 steps
75
158
800
222
36
38
452
124
690
742
4
271
726
375
633
806
368
466
384
101
672
204
488
14
587
574
825
732
539
971
570
992
16
852
857
698
237
338
706
579
675
821
364
746
484
971
86
839
118
873
572
135
538
118
645
792
743
205
585
270
431
195
262
779
335
235
122
84
251
816
412
764
238
636
169
6
993
728
956
270
611
640
593
133
980
26
68
266
927
394
822
879
879
284
896
634
990
158
542
735
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