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.

923
33
578
309
211
47
691
443
418
334
425
13
97
968
124
419
839
663
267
441
726
869
1
91
142
524
863
670
930
416
59
723
98
526
203
169
625
695
462
362
91
175
741
334
822
940
917
860
709
462
826
630
423
246
305
32
219
83
270
539
19
128
118
996
234
404
9
339
375
339
511
700
202
775
286
73
469
510
552
481
404
441
218
820
649
193
556
14
407
689
24
44
143
19
656
788
84
103
789
258
BubbleSort - 0 steps
17
165
662
483
432
131
498
82
176
147
766
383
824
27
290
965
635
106
874
415
353
142
490
888
47
454
354
831
518
699
717
276
433
556
941
768
263
742
41
11
241
16
779
857
767
580
702
455
644
671
19
843
235
276
465
877
907
812
372
474
934
157
409
877
101
898
548
220
338
993
42
314
859
305
720
388
584
864
877
355
498
171
735
855
234
82
970
636
190
283
384
959
185
142
325
705
932
667
163
119
InsertionSort - 0 steps
165
672
915
814
896
370
510
442
909
455
546
248
392
801
732
149
729
520
135
268
43
954
69
104
703
422
169
297
409
629
650
921
685
497
852
349
210
69
213
817
111
804
845
374
172
717
924
176
565
278
853
623
635
127
957
947
248
154
630
508
25
905
222
610
262
109
289
342
568
723
412
840
885
948
783
592
417
183
62
938
540
74
713
722
360
92
388
234
425
886
854
35
372
255
627
824
997
5
332
813
ShellSort - 0 steps
29
958
386
557
986
359
435
508
4
546
148
186
981
239
813
970
3
586
491
48
988
845
380
387
210
318
67
304
383
270
98
282
720
220
142
762
807
664
270
218
693
120
209
986
573
800
529
613
356
144
108
401
25
258
404
100
725
515
819
79
423
259
949
946
970
712
417
705
175
695
121
856
863
816
17
63
977
342
317
89
562
128
56
329
137
93
595
394
787
735
893
763
865
880
773
75
491
309
624
948
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