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.

802
610
295
743
99
430
998
796
964
242
861
63
945
38
123
336
367
222
956
458
23
547
818
212
411
485
299
475
791
918
372
545
312
807
154
985
703
350
968
68
534
264
573
164
99
446
758
723
176
97
927
573
72
391
646
198
840
327
29
137
727
497
103
816
863
667
691
850
297
944
402
262
413
959
370
408
81
804
44
892
16
865
214
635
113
872
269
439
757
702
141
193
603
837
624
396
827
581
757
473
BubbleSort - 0 steps
671
821
235
765
766
269
699
22
457
491
668
785
295
992
789
451
818
60
638
382
300
643
233
398
759
250
424
308
16
515
373
324
589
704
516
749
442
991
102
764
278
429
260
873
789
255
904
772
22
886
768
279
372
871
346
405
302
378
269
548
576
124
157
43
679
657
920
622
698
99
221
833
163
534
717
566
693
177
778
979
207
119
478
648
603
277
4
759
42
70
437
503
139
969
811
255
423
288
687
526
InsertionSort - 0 steps
168
685
518
343
415
167
212
58
251
443
367
425
32
294
615
900
940
158
901
612
11
610
776
270
274
247
762
494
426
587
434
487
846
894
508
290
910
733
81
869
208
407
670
912
795
335
939
43
657
654
611
349
742
851
578
507
775
643
258
167
177
94
464
971
292
551
706
474
456
716
413
459
779
391
998
669
726
54
479
283
32
825
674
185
640
926
288
668
22
547
446
88
809
181
309
911
991
504
239
360
ShellSort - 0 steps
78
835
561
493
892
716
581
402
928
612
532
887
803
288
562
42
783
385
313
399
749
614
876
227
916
89
938
310
36
516
589
683
827
291
327
400
261
135
688
809
575
108
328
134
85
233
833
603
524
99
997
302
471
610
491
914
829
260
479
483
265
556
477
940
143
737
124
407
297
66
149
607
861
236
129
222
444
129
247
419
743
318
37
132
504
122
799
37
836
289
343
161
541
420
823
386
728
772
112
576
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