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.

959
388
201
813
879
646
352
324
985
979
338
668
117
95
472
179
355
828
469
955
740
451
701
926
186
900
858
467
94
190
569
484
860
997
117
929
191
958
607
246
796
331
326
1
671
55
65
810
264
804
289
439
851
351
978
500
559
751
867
525
915
585
140
934
747
186
88
470
677
626
860
791
405
269
183
865
850
45
690
231
208
491
975
192
186
39
58
321
636
472
864
669
992
551
817
815
832
822
1
694
BubbleSort - 0 steps
832
71
168
249
233
493
723
460
332
732
958
644
880
831
402
349
746
375
134
744
264
261
474
481
267
747
841
967
411
74
346
184
192
7
75
795
138
688
709
160
661
789
57
299
73
727
588
771
407
714
149
725
242
906
661
8
928
825
122
144
22
959
178
742
316
214
983
725
652
39
775
178
300
999
654
598
160
840
218
973
352
353
980
438
236
499
841
128
381
360
852
655
397
210
713
761
972
514
793
500
InsertionSort - 0 steps
641
827
957
350
350
888
489
981
49
814
380
153
173
937
324
252
151
829
587
917
794
270
776
114
151
303
138
280
596
72
228
146
535
203
298
256
745
410
310
762
393
952
225
66
798
31
289
594
595
521
273
273
524
495
152
15
982
88
803
81
465
814
114
982
498
905
30
1
223
80
855
191
336
490
946
228
748
237
627
378
302
593
163
607
742
927
333
635
79
268
265
923
337
11
565
618
488
600
220
879
ShellSort - 0 steps
125
895
746
911
996
632
657
395
998
902
736
689
909
393
293
526
339
650
880
489
141
231
238
726
259
614
791
740
453
904
794
913
399
281
322
383
115
886
321
617
592
125
891
170
625
393
469
257
197
967
591
290
360
633
163
135
580
600
370
871
473
564
185
62
39
864
528
591
966
920
480
497
344
668
506
740
979
34
881
563
640
983
345
874
561
455
176
12
758
631
16
137
929
732
186
113
591
200
343
626
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