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.

455
923
514
458
277
189
512
172
167
504
970
448
80
670
980
89
984
928
582
371
965
997
58
707
469
321
953
360
175
530
386
673
607
829
191
861
333
360
54
979
353
487
950
680
332
198
486
808
103
963
973
512
258
231
141
84
236
119
651
679
704
725
629
159
89
920
112
458
850
89
102
248
568
598
101
4
565
290
616
820
657
874
953
572
657
844
518
756
246
388
758
1
772
496
171
307
355
459
92
523
BubbleSort - 0 steps
652
181
694
65
443
300
835
533
408
868
65
50
322
943
893
450
459
280
412
425
590
852
80
390
821
527
907
356
484
886
322
672
281
258
492
997
170
805
835
773
849
826
968
177
71
384
660
690
530
466
672
24
336
878
848
376
163
380
253
319
999
85
182
89
869
576
359
99
737
958
398
177
111
331
564
182
832
29
397
223
904
766
364
151
210
816
579
745
614
805
612
611
997
924
608
190
940
270
142
542
InsertionSort - 0 steps
174
899
312
2
957
770
535
847
16
815
948
366
173
373
811
877
44
345
958
7
213
70
566
103
200
58
581
727
347
993
424
185
368
61
778
342
87
519
611
941
460
954
161
140
228
985
206
134
94
312
656
12
904
903
319
203
373
164
86
313
378
338
339
261
122
750
865
953
389
980
469
818
736
356
547
362
387
48
372
870
948
618
543
930
191
998
731
562
59
128
589
496
251
714
764
374
233
353
66
85
ShellSort - 0 steps
997
761
780
350
419
96
604
518
111
392
854
79
415
556
138
272
987
267
638
300
237
798
191
51
749
726
107
346
80
689
146
215
60
16
696
435
889
183
14
381
608
701
166
715
234
972
102
899
343
327
226
747
297
188
729
650
552
756
816
419
619
906
185
169
834
848
212
27
459
394
832
952
791
135
889
510
93
154
304
862
575
984
978
692
894
217
954
121
44
246
759
123
173
453
268
554
42
934
901
539
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