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.

281
804
718
976
927
209
720
995
155
673
230
759
813
589
101
688
826
221
321
43
339
883
664
973
400
963
588
731
198
942
341
776
174
834
881
777
249
269
869
107
842
241
902
357
800
748
91
97
499
468
956
54
637
777
330
307
63
877
238
139
978
217
606
811
832
311
280
204
52
489
918
20
966
444
58
717
641
859
982
378
938
156
390
732
613
860
473
356
40
87
191
362
595
946
284
766
229
520
5
745
BubbleSort - 0 steps
391
332
530
734
965
207
513
916
287
74
48
93
458
201
537
955
789
529
352
35
997
123
348
902
329
856
635
584
454
242
974
289
180
956
209
466
610
326
807
690
780
444
2
757
116
650
803
460
628
46
677
156
565
68
567
37
721
245
226
382
459
28
532
225
370
789
47
743
238
790
141
195
634
894
882
684
398
132
605
374
3
361
16
301
426
762
644
601
547
771
53
645
190
906
293
875
634
60
577
908
InsertionSort - 0 steps
278
487
398
879
556
257
781
605
299
502
562
367
331
725
17
725
378
478
759
996
173
81
105
635
998
187
74
501
125
522
962
460
18
733
1
851
55
896
508
536
522
545
880
123
522
694
572
879
507
144
975
958
93
770
434
503
477
462
964
658
17
41
988
549
447
365
257
738
435
810
597
841
618
496
658
867
888
565
993
307
864
627
71
154
788
758
87
340
472
7
934
261
373
602
886
664
660
156
971
224
ShellSort - 0 steps
823
655
778
636
115
764
398
846
178
764
839
511
123
888
814
222
832
277
790
946
196
494
555
688
703
574
781
187
743
77
51
512
228
17
54
826
924
419
687
462
457
567
430
94
586
848
797
808
952
418
167
810
298
437
931
102
715
315
412
720
109
983
289
148
336
128
204
814
493
576
591
990
734
817
493
285
641
11
313
419
521
401
578
733
114
418
566
719
222
97
71
67
513
207
616
141
751
456
243
688
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