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.

613
219
334
703
972
631
133
97
907
616
118
596
238
496
674
984
873
254
380
161
558
735
111
554
893
599
401
290
478
975
754
880
572
499
221
153
863
14
220
631
141
692
151
1000
519
34
827
108
267
547
771
518
84
485
380
20
755
861
601
716
970
140
608
385
844
792
257
540
237
180
47
882
2
940
809
550
975
302
478
680
620
619
617
775
164
483
672
410
199
280
92
852
870
643
515
957
756
635
258
466
BubbleSort - 0 steps
611
956
707
598
366
208
208
347
996
938
718
697
455
8
979
314
913
109
983
746
421
407
591
711
150
976
633
482
751
365
29
122
270
633
754
108
79
833
632
607
207
408
834
389
148
459
905
841
955
946
76
546
944
322
672
285
763
490
323
634
448
265
666
447
917
829
812
238
522
203
776
86
41
90
520
410
35
987
165
345
638
907
115
72
99
696
494
779
872
298
146
672
949
990
859
88
548
634
157
646
InsertionSort - 0 steps
855
813
15
491
947
536
500
372
856
850
579
58
552
62
816
656
599
348
638
610
569
470
439
988
250
998
144
880
391
485
276
880
261
4
791
301
860
902
558
934
599
120
775
567
278
863
960
357
983
766
536
115
463
488
701
688
916
291
125
20
523
200
341
488
20
259
920
842
413
847
524
321
136
927
415
996
329
751
880
328
114
645
588
88
392
350
331
559
418
364
669
667
941
412
169
333
381
228
178
17
ShellSort - 0 steps
221
674
861
845
245
178
116
437
437
536
515
352
862
850
366
589
907
230
487
841
405
633
24
707
698
971
994
608
115
251
382
792
584
246
43
790
364
214
304
905
403
594
647
170
162
653
180
563
416
148
618
592
74
295
912
918
249
438
971
33
347
244
162
522
511
463
405
961
242
817
696
955
194
984
590
889
649
758
872
412
796
881
937
107
546
771
24
797
35
988
957
72
949
381
887
491
869
377
859
403
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