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.

57
333
373
216
476
59
676
55
587
1
41
155
546
121
406
183
688
819
24
312
33
448
43
441
182
213
922
685
86
503
834
651
652
241
212
501
954
447
604
655
56
3
473
573
604
974
842
912
724
309
303
951
891
126
342
509
37
295
875
337
313
634
807
584
970
530
342
672
926
267
636
459
539
467
495
432
989
981
629
374
520
464
741
34
559
483
275
252
388
578
810
212
232
757
782
934
229
410
723
71
BubbleSort - 0 steps
763
722
389
39
255
511
602
715
362
469
318
944
643
361
151
310
213
705
189
447
826
146
310
208
430
351
490
846
563
48
369
326
13
649
317
671
613
195
726
787
606
174
770
483
825
155
337
594
751
729
32
199
561
318
930
478
817
758
325
340
656
634
49
619
583
178
853
200
853
336
151
863
117
262
405
350
324
737
998
992
161
582
171
406
877
558
708
759
674
450
401
835
280
22
896
62
267
572
810
201
InsertionSort - 0 steps
588
211
654
66
741
180
757
799
611
984
912
373
480
949
60
459
781
459
928
935
758
858
532
819
260
470
461
478
11
487
597
942
653
762
937
72
485
684
709
985
640
496
747
601
805
371
586
982
460
960
371
708
9
592
183
569
640
743
328
537
967
224
473
661
797
917
374
790
726
390
253
612
633
864
390
796
954
127
396
238
1
168
528
169
833
930
23
461
176
782
355
203
752
46
268
984
893
791
866
138
ShellSort - 0 steps
283
914
343
263
582
11
152
649
176
605
604
868
108
592
320
389
780
454
970
329
969
924
845
452
739
407
683
321
197
822
776
327
358
761
225
191
527
665
654
566
255
266
904
907
791
230
104
509
142
413
663
998
849
96
69
489
636
38
399
774
759
959
78
632
927
109
949
111
825
40
508
249
943
406
369
486
515
39
678
607
906
837
80
653
926
756
672
836
108
236
603
796
46
16
817
603
363
107
784
915
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