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.

660
94
998
238
75
421
269
512
972
640
200
584
651
459
171
230
730
400
276
713
596
818
525
423
359
970
662
947
89
466
923
454
86
589
972
672
480
260
397
406
734
980
44
952
715
12
494
126
622
161
143
446
640
363
972
301
306
27
997
148
722
725
322
377
755
992
935
136
624
748
506
957
591
612
989
253
973
519
713
260
803
687
330
305
382
855
298
181
38
657
805
974
466
682
212
420
274
333
36
856
BubbleSort - 0 steps
605
316
132
720
377
777
373
599
42
893
307
827
466
142
683
70
42
225
444
200
431
209
404
817
504
229
305
578
192
710
832
791
572
377
485
596
605
52
855
26
860
642
616
583
209
529
686
1
483
127
721
357
648
417
55
157
290
807
486
844
329
992
907
674
539
205
172
816
817
919
890
811
569
946
231
529
272
98
133
773
3
3
782
849
181
974
753
831
806
438
750
922
635
340
494
956
436
675
627
163
InsertionSort - 0 steps
239
66
397
511
7
69
659
501
982
290
268
456
369
513
748
292
236
947
413
396
334
385
78
134
214
655
420
613
363
447
4
948
861
29
171
104
948
518
390
454
505
424
328
538
166
742
771
392
425
42
576
18
36
61
385
500
545
845
4
369
872
644
956
638
407
743
750
821
728
161
638
44
231
755
372
769
844
876
521
818
58
943
905
154
702
882
20
525
662
355
671
457
197
334
775
783
886
590
131
501
ShellSort - 0 steps
471
583
742
212
364
353
908
479
559
531
388
908
108
469
558
49
770
322
900
475
801
3
907
184
578
266
888
309
817
872
178
429
638
685
446
820
587
875
6
345
89
529
976
334
390
298
525
815
983
365
518
433
803
84
146
691
275
650
149
606
468
561
724
438
562
788
758
773
783
813
998
578
326
803
986
241
412
274
600
933
148
679
550
28
659
914
247
290
433
705
712
700
56
457
764
151
101
306
386
842
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