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.

641
500
1000
834
390
959
928
387
411
533
143
428
318
296
664
943
969
675
972
897
372
179
584
759
137
578
345
811
4
11
913
360
671
952
530
361
376
531
173
764
774
109
700
630
571
365
670
994
560
159
539
180
572
223
173
898
387
701
112
976
141
916
859
388
625
790
361
597
882
272
61
124
946
343
722
613
544
277
902
23
66
590
862
822
645
265
550
895
932
826
102
224
617
439
709
842
884
264
146
66
BubbleSort - 0 steps
863
131
284
393
889
22
638
822
406
11
596
765
388
57
736
471
575
268
846
94
766
304
126
44
464
536
724
146
557
251
91
939
409
572
145
285
292
326
631
140
112
453
361
703
437
634
297
137
500
531
337
564
900
248
732
461
74
209
521
533
646
738
845
485
603
260
632
564
459
867
951
341
661
691
596
70
856
758
830
514
363
884
325
912
264
136
585
831
220
303
705
544
195
180
248
630
202
291
536
28
InsertionSort - 0 steps
558
345
644
439
841
870
128
896
919
97
701
395
34
293
378
132
736
47
925
433
242
990
853
321
105
596
668
625
196
920
89
412
144
476
369
204
106
846
278
806
605
134
68
22
599
678
577
48
830
15
791
700
311
277
21
143
504
197
701
421
136
430
367
646
79
507
429
316
120
625
332
819
888
848
595
220
798
874
395
107
89
684
260
884
623
132
130
315
293
664
553
645
194
986
859
927
922
237
872
525
ShellSort - 0 steps
154
192
114
455
493
377
46
905
339
739
455
69
394
173
536
317
506
952
159
196
625
758
507
169
291
108
488
34
465
663
73
511
73
727
75
572
234
810
415
640
536
182
444
626
103
905
745
463
361
568
474
11
208
624
916
131
984
61
689
833
840
730
589
959
554
917
578
9
310
785
928
426
471
844
977
112
740
943
230
299
489
253
597
310
564
754
392
959
726
687
63
613
181
607
933
1
105
525
279
754
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