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.

164
252
673
625
861
518
347
754
465
223
989
367
663
649
592
241
675
633
256
711
738
651
886
498
359
363
615
901
292
331
476
333
301
877
965
33
189
371
789
939
391
202
539
100
344
8
962
453
966
110
450
642
295
337
530
512
719
496
805
283
201
813
126
483
92
434
831
226
548
510
641
473
51
226
222
313
754
54
997
893
130
937
247
443
3
922
923
899
193
666
418
461
800
785
950
913
78
67
105
562
BubbleSort - 0 steps
63
194
460
514
743
893
231
248
123
356
390
875
454
153
711
860
908
193
463
405
390
623
270
12
371
239
19
932
734
151
629
12
591
211
244
797
556
408
34
440
75
444
509
345
334
776
499
824
644
859
501
608
807
532
582
344
730
914
460
119
779
236
384
137
750
714
474
362
855
228
620
608
385
36
227
638
621
440
468
570
385
545
363
217
855
951
214
735
639
335
277
755
18
408
766
571
226
64
752
117
InsertionSort - 0 steps
22
363
292
635
16
323
109
82
620
189
264
453
475
558
869
485
197
84
973
643
462
344
219
775
260
800
239
49
24
672
363
584
573
702
524
679
628
653
394
1
620
880
495
599
988
965
911
941
972
336
595
231
697
470
349
871
7
145
78
143
930
73
804
973
523
955
684
93
827
740
208
697
351
603
634
625
987
197
442
654
770
749
170
572
587
743
683
737
492
268
266
550
519
688
230
444
490
724
148
610
ShellSort - 0 steps
713
227
265
504
977
805
394
685
801
478
766
182
459
682
57
964
868
960
584
518
204
115
962
308
121
242
235
58
355
333
385
889
321
682
657
57
931
890
574
216
863
216
723
249
153
163
486
448
880
953
601
805
933
67
115
35
788
125
117
57
846
631
483
9
306
767
724
918
376
473
186
859
594
640
123
102
537
330
70
591
513
503
587
747
262
416
124
963
735
451
707
957
43
405
717
72
619
919
451
209
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