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.

398
217
329
800
487
966
382
298
135
321
453
533
519
397
575
921
276
371
942
182
368
997
558
377
407
493
364
112
366
202
522
940
102
536
149
65
519
855
15
16
360
568
237
199
812
874
278
202
881
186
272
514
148
653
15
751
419
697
998
93
598
85
493
382
849
66
641
947
191
931
508
739
729
734
183
484
843
638
686
469
259
822
184
590
270
525
884
859
126
727
390
224
993
969
22
231
268
726
555
796
BubbleSort - 0 steps
908
294
915
190
82
407
84
33
749
242
890
538
614
870
767
207
345
127
724
292
444
543
103
457
810
10
509
786
291
72
758
690
341
625
560
907
343
999
96
615
919
944
186
857
934
13
859
912
8
551
398
618
434
256
44
949
913
697
99
907
349
43
631
15
862
419
237
638
519
760
135
659
216
510
414
582
657
89
187
770
715
681
98
871
838
84
606
364
203
753
421
509
519
881
603
236
746
844
737
318
InsertionSort - 0 steps
757
9
260
229
470
51
543
757
611
587
848
252
158
825
894
487
49
165
407
487
413
997
854
95
639
408
473
784
99
303
27
628
105
244
310
106
440
707
15
140
632
224
980
329
757
339
869
469
150
116
583
666
669
140
831
728
974
532
754
480
383
562
418
263
591
627
993
355
277
470
689
417
792
970
514
72
998
857
765
278
850
391
260
220
907
536
845
964
177
523
20
222
936
188
923
576
381
77
250
323
ShellSort - 0 steps
690
443
215
639
970
28
737
355
346
909
738
637
426
362
775
404
220
658
537
468
616
232
714
483
20
860
520
465
337
555
46
439
361
288
571
114
463
998
154
280
289
313
280
713
206
947
400
573
34
35
52
253
123
95
700
647
427
648
174
404
803
789
863
782
186
640
99
192
122
810
436
984
237
243
368
344
920
742
403
970
976
331
771
724
421
703
986
834
127
68
48
924
469
476
197
917
793
499
452
268
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