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.

599
75
335
665
352
328
375
403
880
465
813
214
840
663
734
623
556
36
707
878
466
958
336
216
485
521
368
243
342
428
303
520
909
273
621
542
110
237
288
982
955
988
316
253
41
764
870
951
226
54
746
648
101
507
750
120
833
678
393
972
498
992
601
906
433
641
14
351
683
946
297
761
886
228
342
458
921
351
525
292
499
33
881
598
219
801
262
568
818
909
358
70
234
239
664
541
734
609
292
245
BubbleSort - 0 steps
426
849
685
97
86
771
155
139
926
915
880
439
104
307
221
379
242
873
453
725
100
352
335
618
843
990
811
456
528
875
805
624
507
724
527
324
998
247
862
838
664
777
615
353
312
819
242
319
700
257
481
521
880
673
349
283
568
485
477
460
838
200
234
242
779
66
915
562
544
600
210
202
293
556
805
207
682
536
350
590
798
564
760
51
590
272
854
483
409
708
294
509
528
966
167
854
707
653
526
541
InsertionSort - 0 steps
524
434
875
948
751
587
361
790
358
818
625
916
401
571
63
598
861
154
195
791
417
926
484
74
354
971
543
794
585
766
853
114
750
690
925
385
561
832
502
804
614
30
845
339
240
980
213
986
76
606
497
831
786
715
979
543
431
987
79
709
878
893
528
864
39
457
300
427
789
294
682
586
629
437
779
767
186
356
649
674
132
926
342
985
454
459
346
60
248
977
30
924
157
534
973
404
403
231
954
617
ShellSort - 0 steps
36
548
758
503
711
261
363
863
363
430
171
534
791
19
526
951
677
478
118
260
789
289
558
928
412
4
285
308
924
651
71
758
858
562
230
544
85
39
215
969
110
458
796
758
747
216
86
410
970
216
208
159
39
282
655
289
378
91
863
850
726
859
710
494
341
731
834
314
256
785
441
547
575
953
395
181
492
92
729
26
209
886
17
914
643
348
907
572
246
187
571
995
297
773
987
273
166
637
62
838
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