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.

338
670
987
866
701
372
516
980
2
432
516
345
78
963
380
905
461
130
287
481
510
688
51
464
532
896
857
906
282
405
625
991
941
264
388
934
116
675
273
873
652
319
613
834
658
388
961
768
581
580
105
726
582
182
926
188
725
48
194
523
358
206
332
627
904
908
530
214
688
273
406
295
520
239
972
255
134
145
151
264
103
821
5
98
143
13
865
697
365
828
993
779
414
252
417
885
361
95
541
378
BubbleSort - 0 steps
346
481
794
860
196
910
307
462
945
755
935
998
667
547
750
450
49
89
893
523
430
269
640
210
420
669
881
395
734
50
591
708
486
789
973
201
887
713
726
455
89
222
33
835
42
647
565
944
295
692
855
171
34
876
587
940
631
816
926
902
631
207
953
434
860
745
80
880
312
253
146
753
440
291
14
165
510
537
886
23
108
736
324
639
116
738
555
267
727
377
989
383
469
470
167
995
938
772
133
358
InsertionSort - 0 steps
249
111
468
420
592
982
142
542
614
48
117
712
671
358
815
299
750
969
321
770
563
9
550
996
794
559
825
345
835
517
309
16
971
693
515
569
301
869
547
735
496
225
730
619
622
360
347
186
342
441
99
930
190
44
964
233
503
713
675
595
706
397
43
697
272
678
205
197
127
483
708
155
463
54
895
731
492
764
38
103
705
425
129
638
781
320
312
612
275
217
569
271
118
114
986
795
371
540
917
126
ShellSort - 0 steps
344
793
284
297
300
711
723
394
291
762
412
784
760
317
27
560
910
881
972
986
289
568
980
117
256
625
856
41
408
851
440
647
333
278
284
603
269
910
861
263
638
820
481
542
416
98
105
110
483
322
486
505
557
972
9
817
691
67
704
624
642
317
909
363
575
150
398
918
618
636
578
152
444
876
587
7
558
575
775
965
616
145
170
957
173
423
501
462
325
872
611
551
229
539
32
996
702
539
281
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