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.

279
298
665
826
153
200
214
495
335
842
688
307
310
989
831
789
319
430
520
677
931
137
166
359
973
490
242
491
692
332
453
122
58
622
909
248
965
286
597
674
84
213
25
258
775
546
366
3
922
261
72
772
475
312
486
848
279
225
992
243
720
273
371
396
372
486
808
649
486
856
649
959
397
440
174
935
581
694
108
917
536
949
821
902
275
541
720
940
498
777
474
348
943
862
383
128
149
885
755
837
BubbleSort - 0 steps
759
146
951
160
117
724
124
532
642
487
580
944
254
128
449
267
500
374
324
117
423
890
126
356
79
708
723
605
214
426
242
857
224
899
872
101
222
997
861
992
290
990
418
686
763
919
545
824
479
49
507
842
769
630
574
271
387
336
759
665
682
703
274
15
998
126
708
729
631
158
45
328
826
716
682
893
435
946
120
265
188
509
922
222
456
70
820
587
112
79
957
580
683
193
322
125
254
114
425
453
InsertionSort - 0 steps
894
492
537
966
65
814
729
12
700
843
147
715
528
500
246
700
243
214
450
212
465
541
89
859
60
915
879
326
802
591
985
793
7
847
456
140
661
832
568
239
192
351
71
987
128
164
324
761
278
310
208
904
9
918
677
77
623
994
457
985
444
9
22
548
328
625
252
546
365
929
808
911
850
803
990
795
905
59
777
826
985
219
469
145
265
665
247
838
3
659
756
521
55
668
851
875
70
368
934
833
ShellSort - 0 steps
129
110
673
763
394
490
816
415
21
551
249
495
518
897
575
215
570
732
61
549
564
795
884
852
198
212
299
77
392
51
433
658
217
612
221
166
71
262
203
923
778
106
6
947
42
817
553
411
448
286
646
62
667
950
223
903
454
722
776
890
499
779
225
908
619
264
397
439
481
794
370
791
139
905
158
915
209
533
718
686
810
596
674
336
936
279
427
143
57
625
663
706
291
348
580
304
890
852
18
204
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