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.

746
378
279
739
274
400
928
943
942
316
769
188
242
827
401
629
238
176
739
781
189
914
38
818
789
248
685
959
631
408
579
703
78
42
46
851
383
352
375
726
110
245
337
952
453
362
36
75
599
461
516
80
822
460
753
455
342
107
516
130
136
153
611
243
592
235
769
451
293
955
651
975
193
367
284
593
507
271
124
707
884
618
403
249
238
215
440
974
852
482
996
455
296
545
170
772
602
966
520
7
BubbleSort - 0 steps
767
619
113
3
672
218
321
730
19
264
9
833
864
133
726
769
439
196
558
362
885
627
398
9
987
681
297
537
239
845
856
563
475
186
239
243
359
961
717
357
141
42
308
92
486
668
116
392
962
866
935
65
781
381
392
854
325
621
290
428
525
384
423
953
393
11
956
784
602
243
989
825
932
236
767
192
973
24
204
811
446
86
3
239
30
798
911
105
691
108
659
841
908
851
740
845
944
51
534
531
InsertionSort - 0 steps
631
173
49
246
440
341
433
891
233
973
845
317
213
936
80
253
508
283
762
200
678
627
295
907
3
843
899
679
209
134
775
80
187
979
137
179
346
147
254
816
398
570
485
631
793
411
140
12
954
45
204
910
481
827
126
230
366
294
43
380
815
727
970
249
842
915
386
830
982
363
101
674
875
84
468
492
463
730
513
237
444
487
431
300
26
638
58
719
999
389
658
841
722
6
58
187
54
152
421
156
ShellSort - 0 steps
217
560
981
737
242
698
503
564
801
690
796
200
704
159
426
781
110
901
403
167
973
973
855
117
912
359
788
330
786
35
26
684
787
27
339
209
919
420
69
64
341
7
272
396
694
427
765
501
584
684
507
831
699
233
357
99
64
810
636
661
906
609
745
592
689
619
188
494
400
566
907
847
120
239
116
66
600
396
393
129
829
951
146
882
619
623
687
237
704
882
130
398
372
4
468
266
488
830
655
137
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