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.

317
805
864
574
475
422
175
951
359
248
34
607
783
110
271
525
944
285
590
498
79
925
180
254
485
466
66
809
765
112
437
476
245
194
796
685
689
9
640
220
243
411
236
314
975
635
6
479
329
18
734
976
591
761
925
203
755
58
51
657
767
92
216
584
358
844
912
68
755
989
773
23
896
989
587
228
746
338
251
799
455
346
138
382
248
224
725
97
330
293
377
374
212
434
277
607
898
104
208
58
BubbleSort - 0 steps
349
718
129
57
749
22
351
986
617
703
282
596
725
392
746
542
232
884
930
451
507
392
901
837
801
691
714
652
89
327
639
342
806
561
413
675
203
275
10
80
7
775
151
151
129
275
93
673
753
561
853
980
743
945
987
747
853
48
312
31
642
664
390
623
434
105
115
468
351
868
743
610
605
659
908
474
335
479
224
914
461
779
539
258
358
360
121
27
368
799
831
62
542
333
725
980
451
520
323
9
InsertionSort - 0 steps
83
366
560
968
69
164
687
951
714
91
74
339
66
963
960
455
786
70
753
381
749
506
912
2
77
245
235
179
103
667
607
110
671
16
364
643
401
238
904
117
249
520
330
830
251
367
219
684
789
542
487
131
899
575
636
869
965
372
509
110
543
950
496
1000
104
571
467
952
392
148
150
457
883
181
616
498
311
818
539
501
145
311
299
784
218
89
355
727
174
277
616
542
136
928
559
206
421
713
359
261
ShellSort - 0 steps
783
167
175
484
584
710
399
993
212
206
527
154
974
365
891
89
767
503
695
74
994
712
837
17
286
103
272
440
200
29
623
124
691
794
14
776
262
809
156
236
425
702
653
373
939
629
177
43
934
955
423
784
243
572
364
29
404
861
537
927
622
487
7
366
133
154
141
316
436
777
156
213
478
620
244
463
685
666
539
271
654
66
658
716
735
512
10
168
383
473
4
774
6
256
469
177
265
855
814
208
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