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.

605
728
806
732
881
706
357
427
463
615
930
85
902
447
164
600
669
699
463
555
184
69
626
988
588
791
457
758
172
20
358
880
900
461
139
860
880
619
281
634
169
126
773
297
752
181
947
429
268
809
425
809
640
122
273
156
992
165
48
359
443
450
359
922
800
318
354
883
330
673
41
89
200
578
720
229
67
865
855
981
523
103
530
221
184
96
77
165
894
937
130
692
325
542
698
57
419
292
823
470
BubbleSort - 0 steps
186
371
546
561
814
596
461
730
902
264
33
267
207
734
643
960
638
896
109
710
427
54
831
928
2
541
59
964
927
329
587
760
767
523
840
458
300
774
148
668
544
210
196
717
376
738
435
261
327
448
450
607
689
360
36
785
678
368
665
972
792
274
728
593
182
345
994
155
195
73
489
72
293
659
986
769
342
154
205
355
727
278
402
612
950
862
478
753
419
693
161
273
736
620
447
285
403
192
205
10
InsertionSort - 0 steps
792
282
438
219
983
258
811
575
372
134
453
395
701
714
619
733
715
557
707
654
684
317
290
545
194
216
946
477
673
325
239
910
969
493
529
578
597
473
49
760
938
678
210
193
399
952
339
472
524
614
575
376
328
869
35
998
505
72
596
467
109
303
796
701
42
484
803
246
588
183
74
582
991
86
153
382
576
318
511
995
563
990
582
999
949
180
330
961
251
266
340
79
458
977
333
866
865
979
947
1
ShellSort - 0 steps
685
901
463
383
459
864
11
534
390
616
545
914
793
341
923
479
774
957
35
813
779
507
996
862
727
485
126
523
226
997
798
49
85
496
441
439
282
962
404
860
984
681
975
752
84
871
157
823
564
775
270
872
92
551
931
737
342
513
104
276
98
478
420
422
865
205
454
605
267
519
453
763
63
322
580
783
64
150
34
343
669
682
358
539
9
764
364
598
739
796
753
252
930
444
763
494
502
297
552
366
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