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.

313
871
659
422
895
380
152
654
569
946
743
8
830
931
393
8
529
651
54
246
192
879
241
224
631
742
892
343
90
247
881
540
514
848
772
57
813
153
968
567
697
821
583
181
931
227
392
869
143
777
654
268
618
302
475
322
356
641
353
601
67
895
93
996
646
656
463
633
320
820
457
194
334
866
337
664
977
261
887
731
6
719
883
355
27
706
818
109
968
455
350
629
310
559
438
257
695
290
922
856
BubbleSort - 0 steps
984
478
434
486
341
793
864
44
170
553
906
432
578
644
901
349
369
812
541
332
239
876
409
357
543
123
258
777
549
228
276
859
832
249
242
644
616
668
168
632
589
73
501
768
881
610
555
209
241
847
274
598
981
930
52
153
607
813
74
45
285
829
941
464
832
752
436
633
831
471
839
467
908
131
244
490
430
337
560
440
698
503
219
643
83
111
512
155
479
931
482
110
576
755
107
592
59
609
657
199
InsertionSort - 0 steps
281
524
758
130
269
915
864
234
976
656
311
119
786
103
8
119
286
608
294
28
306
240
594
199
954
186
542
306
522
49
247
950
31
902
57
297
647
791
5
505
711
746
24
666
280
534
149
171
725
357
372
507
61
843
445
870
311
922
398
272
236
357
624
369
784
704
558
22
72
912
772
460
293
116
4
867
333
626
933
391
109
107
816
646
582
213
109
366
689
846
847
848
70
201
964
444
788
696
831
944
ShellSort - 0 steps
851
986
245
819
694
841
119
908
215
46
93
42
119
951
936
14
243
478
424
528
106
350
267
722
841
452
504
294
198
546
707
666
372
327
122
825
549
870
158
457
315
990
389
275
845
91
494
71
234
37
416
693
207
337
753
258
870
507
345
609
322
925
172
888
632
533
221
793
81
729
310
279
355
592
156
233
929
551
191
663
771
381
917
547
696
720
226
40
809
392
867
137
962
569
460
979
914
264
867
655
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