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.

987
419
732
297
840
887
307
371
643
375
690
547
113
344
264
674
43
276
883
607
846
131
853
608
941
17
958
33
925
120
194
647
483
829
329
195
715
294
851
716
4
672
813
471
943
713
487
508
844
180
373
1
239
881
400
237
416
188
267
940
123
636
339
248
951
113
641
604
689
976
429
752
819
80
171
923
535
817
279
487
102
424
147
973
633
905
816
204
86
845
295
492
365
819
212
100
567
502
26
235
BubbleSort - 0 steps
818
795
986
1
621
995
574
808
133
803
908
895
167
961
423
516
239
907
271
769
385
875
576
432
89
858
881
964
774
415
596
280
332
935
247
811
972
741
90
999
628
488
622
775
775
374
206
883
490
314
590
161
59
807
630
727
869
628
351
763
722
996
425
381
519
480
524
431
930
189
685
903
851
230
786
601
477
999
958
379
796
160
966
862
914
699
470
483
553
506
179
883
426
530
86
836
397
83
506
551
InsertionSort - 0 steps
333
858
296
451
125
284
661
464
332
477
378
926
158
561
910
363
357
295
72
384
427
306
18
770
243
979
570
453
297
521
921
376
384
869
903
380
428
466
660
622
400
522
220
431
406
658
654
415
473
758
853
739
633
199
4
178
426
114
551
157
47
370
164
818
546
222
789
292
402
375
79
702
826
831
898
240
715
246
127
376
227
829
187
166
134
417
828
598
541
953
544
373
380
373
175
427
702
852
287
616
ShellSort - 0 steps
290
266
171
288
835
60
944
343
66
330
970
829
672
798
379
991
559
844
431
107
626
814
566
897
183
541
775
128
81
329
194
320
652
524
118
730
572
293
949
862
391
472
81
733
83
711
438
86
611
744
673
530
297
136
274
134
900
775
534
81
516
681
497
971
542
698
950
325
481
141
135
498
228
461
881
400
909
474
570
988
43
967
324
968
374
788
952
478
788
502
160
544
462
776
209
627
863
446
113
376
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