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.

938
461
924
675
470
221
856
849
175
186
415
81
201
9
622
752
345
284
234
821
955
221
417
511
328
158
332
504
969
506
617
625
428
686
589
184
797
239
752
370
144
166
562
664
525
117
224
328
277
940
296
554
823
21
287
739
460
751
503
922
67
631
170
760
503
607
888
600
334
227
415
608
593
806
821
8
752
367
172
198
670
790
609
254
45
834
718
773
716
696
470
214
872
733
462
425
309
862
194
289
BubbleSort - 0 steps
64
153
482
33
268
2
633
913
100
911
68
53
449
400
654
447
17
184
800
683
3
723
453
575
272
421
863
154
928
579
410
170
214
537
880
922
374
686
322
602
52
725
452
413
221
741
743
468
918
418
403
931
819
930
944
978
956
829
450
113
446
852
582
560
569
448
194
468
489
367
56
664
643
758
499
946
680
788
640
68
277
455
462
762
524
14
913
231
495
727
889
619
699
878
8
703
726
417
355
472
InsertionSort - 0 steps
165
487
627
530
822
190
556
248
368
841
8
243
255
910
245
216
785
205
565
208
12
131
412
298
889
204
887
128
12
239
589
967
511
757
60
178
455
420
559
93
502
767
494
747
821
301
122
151
302
288
903
97
547
164
298
113
563
766
655
298
550
321
305
623
194
996
974
312
909
103
846
618
170
870
439
52
835
554
992
342
596
919
764
521
261
104
225
811
879
702
35
645
77
36
619
744
153
50
796
969
ShellSort - 0 steps
869
578
70
335
429
263
631
60
793
368
907
938
404
895
247
941
76
115
341
273
561
853
107
837
837
209
898
664
845
627
968
411
414
500
363
986
857
824
75
723
720
421
786
553
801
990
910
823
571
615
449
965
663
344
396
285
412
210
497
896
73
384
83
93
939
434
578
494
950
464
27
196
530
151
422
775
145
467
401
901
861
851
349
419
758
497
531
937
501
930
840
263
774
282
928
720
991
371
752
49
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