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.

238
679
264
198
395
170
612
385
674
737
395
270
746
658
643
737
468
366
140
709
465
28
721
664
51
495
355
555
703
294
193
393
36
466
235
453
330
247
674
189
851
401
379
254
375
557
718
530
518
521
108
521
211
523
208
51
832
72
359
281
663
57
720
461
743
116
371
918
254
721
165
527
354
286
540
965
612
537
111
297
844
6
592
688
596
340
192
841
447
484
920
962
280
607
638
540
166
692
591
29
BubbleSort - 0 steps
69
576
607
468
562
633
307
697
562
609
538
584
398
300
642
460
363
105
253
921
955
555
105
246
312
207
134
123
961
338
167
958
361
500
751
102
75
651
693
461
578
511
505
62
272
469
64
449
544
529
96
523
671
869
184
208
993
843
291
477
29
129
59
971
323
868
247
539
446
554
395
158
112
975
199
429
905
478
753
728
928
400
472
943
152
354
776
305
3
478
451
675
524
467
406
959
829
141
417
919
InsertionSort - 0 steps
280
192
823
570
142
563
791
915
967
209
976
895
71
565
242
681
554
271
442
787
825
674
645
100
549
702
935
107
631
806
5
868
261
237
84
781
397
142
84
119
310
811
995
723
267
870
28
485
277
259
531
309
789
530
110
437
180
701
515
573
8
848
51
327
524
878
341
691
7
976
163
984
330
555
962
831
702
659
787
268
627
525
74
94
120
144
116
993
904
372
206
322
47
293
184
2
547
767
920
165
ShellSort - 0 steps
594
72
755
92
754
343
756
248
566
895
883
483
84
552
580
774
449
430
78
454
184
630
538
330
988
162
326
595
82
417
224
1
220
622
377
686
929
840
774
305
133
3
96
394
517
880
139
657
597
47
841
576
26
305
712
30
668
312
223
582
317
285
879
477
407
269
762
435
706
559
682
421
757
707
500
68
580
925
635
785
577
934
950
472
245
9
698
575
36
282
466
396
138
999
923
322
955
331
560
819
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