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.

988
446
590
791
698
681
417
989
152
838
395
605
983
114
113
382
166
586
418
901
478
821
158
971
687
11
306
46
955
125
549
251
414
563
955
429
968
744
51
293
334
172
486
259
402
906
718
872
614
198
459
829
247
555
996
160
422
600
659
123
413
68
293
170
79
603
935
832
898
932
674
277
302
936
708
921
982
867
984
436
482
658
520
791
597
612
599
989
529
585
16
415
709
623
425
997
750
626
772
981
BubbleSort - 0 steps
232
74
637
613
634
71
102
762
271
796
520
642
816
302
712
26
395
439
566
214
688
998
74
456
552
331
668
544
489
252
367
113
361
328
831
942
152
957
951
838
218
984
605
263
440
988
231
378
341
432
295
87
173
663
679
364
384
544
542
643
145
337
171
650
690
102
450
61
511
64
782
739
230
670
201
344
686
813
385
409
474
487
967
499
445
438
211
215
736
3
632
229
616
850
503
987
830
506
352
212
InsertionSort - 0 steps
939
87
126
32
813
302
700
758
401
41
993
795
455
482
693
208
441
13
225
899
432
711
531
73
605
350
222
195
237
171
506
702
988
17
722
863
538
131
740
568
913
842
44
497
190
142
971
665
3
227
631
257
733
170
6
876
524
958
797
12
174
799
365
78
71
22
554
524
558
907
45
800
968
117
738
582
317
24
155
721
924
946
240
729
340
906
833
745
865
678
886
638
222
485
728
109
280
900
480
903
ShellSort - 0 steps
125
205
889
112
394
314
635
83
402
449
758
506
704
680
61
862
312
260
164
759
825
81
515
585
182
500
394
21
799
74
329
380
251
440
119
603
952
89
422
465
193
329
968
483
421
743
969
946
35
608
138
363
621
476
172
322
874
665
238
44
896
334
893
968
815
889
514
957
817
93
234
481
43
297
512
805
654
486
84
232
202
698
586
712
459
134
306
462
506
709
575
245
805
358
287
671
295
583
464
822
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