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.

372
538
325
137
856
281
329
934
670
310
224
476
34
564
764
965
248
141
107
854
778
48
589
559
943
41
532
876
737
448
560
932
59
12
974
746
896
982
630
487
330
693
732
935
149
92
653
617
387
727
262
444
348
45
775
787
916
996
748
876
377
68
61
308
509
798
65
152
827
25
473
352
179
719
559
598
496
624
801
875
940
854
854
215
643
463
880
428
799
259
939
488
904
497
360
408
365
156
115
132
BubbleSort - 0 steps
465
176
771
824
226
223
948
935
41
793
781
973
955
85
178
794
727
639
418
307
491
452
280
959
381
184
320
219
132
300
25
858
542
576
369
539
80
488
239
459
272
411
960
659
728
30
724
332
515
3
650
819
305
550
750
695
11
486
793
142
156
556
243
8
706
463
543
932
279
587
268
679
152
194
555
529
802
310
625
608
596
149
365
766
18
498
187
232
181
573
26
854
901
506
654
30
570
466
204
275
InsertionSort - 0 steps
952
748
888
107
492
264
461
12
97
912
89
655
311
349
562
679
937
523
763
359
454
342
598
315
389
367
431
26
292
343
294
996
108
483
892
664
994
164
11
252
705
572
25
694
618
773
82
88
463
385
678
776
783
63
25
279
888
697
731
704
743
310
559
991
640
606
899
771
297
380
971
677
233
576
477
474
873
355
405
998
352
999
864
102
260
602
379
10
927
844
522
279
714
343
291
625
58
761
23
69
ShellSort - 0 steps
191
34
127
68
520
350
201
188
551
949
875
431
849
654
617
346
740
141
301
147
180
649
910
727
638
604
942
163
87
306
529
222
514
26
119
591
305
14
932
154
176
687
760
497
767
568
818
455
308
943
190
190
554
233
722
192
407
661
192
198
609
559
484
226
542
389
336
742
793
567
606
497
711
431
928
667
85
377
195
41
46
522
667
798
562
344
259
129
923
767
32
218
318
836
407
759
843
644
791
656
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