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.

165
36
43
133
120
537
584
487
170
790
952
3
749
340
739
64
842
447
349
659
61
383
328
584
485
481
4
170
317
683
619
144
694
550
182
693
811
311
499
194
288
79
175
523
324
56
111
941
916
320
525
25
191
210
504
741
600
906
183
10
616
690
230
275
943
846
757
954
129
75
73
124
864
610
643
459
204
871
477
200
320
210
161
201
135
237
337
564
957
710
74
970
720
618
819
543
266
521
359
512
BubbleSort - 0 steps
453
785
182
373
160
218
718
497
332
322
72
28
67
820
591
759
321
683
361
406
396
220
169
889
175
869
811
830
340
66
204
631
861
96
968
919
138
705
86
808
64
74
677
957
904
955
531
111
320
50
270
308
272
795
79
914
241
179
974
702
690
387
685
202
57
840
605
929
983
57
421
35
249
332
926
885
450
612
307
349
606
53
204
200
223
607
889
752
441
260
992
428
713
610
250
802
831
974
137
400
InsertionSort - 0 steps
969
41
27
236
972
435
602
203
773
993
479
179
972
371
985
240
956
769
155
661
319
859
333
278
355
664
207
434
429
831
84
370
826
655
465
54
963
56
418
182
415
441
39
114
423
916
94
781
956
582
687
876
178
305
231
504
17
70
758
423
369
646
395
780
536
897
877
805
372
527
60
419
414
683
291
873
392
447
590
193
704
496
245
56
186
302
78
699
302
868
632
379
560
120
379
998
959
652
559
649
ShellSort - 0 steps
854
833
268
560
742
747
138
271
656
106
627
976
170
15
829
45
804
845
250
541
51
155
356
521
315
490
968
902
936
862
897
871
480
74
65
754
32
497
773
350
449
158
896
654
539
888
164
118
82
16
325
503
928
792
668
449
662
320
875
252
247
958
148
330
415
498
585
878
941
223
401
801
56
814
746
201
395
195
679
508
521
855
905
455
300
382
937
1000
822
253
952
653
93
920
477
478
146
12
764
115
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