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.

131
137
656
839
822
984
372
608
694
632
291
162
508
656
869
216
249
176
180
922
669
280
70
650
518
835
961
333
363
658
686
690
683
570
475
931
542
759
75
227
815
498
100
588
102
21
235
362
142
806
217
1
434
354
566
165
350
573
380
348
639
13
655
385
985
924
617
902
79
607
624
819
33
300
537
995
153
377
574
820
830
191
783
706
320
197
815
506
222
481
291
345
801
730
355
570
128
317
865
269
BubbleSort - 0 steps
919
203
134
273
258
690
600
762
632
303
536
911
897
59
25
909
832
46
358
219
328
659
920
801
604
284
577
569
896
410
886
768
732
674
572
830
558
152
300
637
353
607
457
158
332
663
112
52
362
296
727
317
220
642
787
702
89
539
470
741
144
516
286
188
156
174
888
679
451
727
159
68
517
311
670
196
616
608
786
324
132
927
432
776
142
308
352
448
732
464
661
983
705
470
972
720
929
443
263
863
InsertionSort - 0 steps
226
267
354
28
865
790
723
258
622
255
253
887
970
558
588
243
112
408
346
927
656
788
986
665
81
618
517
928
451
576
689
607
722
193
157
972
143
951
90
914
827
266
854
236
877
150
132
580
732
867
437
527
116
269
406
568
59
302
290
494
480
904
756
659
225
136
946
472
353
281
282
692
3
190
558
64
256
985
159
275
528
884
608
853
337
270
53
83
395
675
556
715
442
925
459
317
565
464
620
256
ShellSort - 0 steps
492
561
26
179
941
728
685
994
993
351
974
319
710
355
567
712
171
47
665
424
942
917
374
304
172
412
447
694
193
933
73
1
133
647
930
486
270
501
830
481
222
159
607
450
804
846
68
704
32
424
227
203
289
499
891
450
456
949
856
922
473
595
912
31
589
799
263
566
362
649
508
492
13
804
300
183
932
138
438
328
201
535
646
596
629
298
989
963
376
241
602
441
579
174
12
277
190
368
695
188
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