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.

88
54
241
512
763
228
699
331
615
731
658
108
688
179
334
625
929
832
434
208
4
609
490
905
846
524
369
199
783
327
108
681
270
364
362
371
362
418
639
832
440
470
901
263
369
209
226
934
521
468
667
283
199
490
959
623
320
55
410
647
410
225
18
894
165
285
285
825
51
663
245
410
319
194
158
36
457
182
586
363
734
613
66
245
560
376
168
841
951
73
679
172
260
565
120
149
817
447
374
361
BubbleSort - 0 steps
354
150
233
105
967
536
262
832
419
480
308
664
111
931
571
488
874
129
307
292
202
690
205
233
173
317
622
344
897
888
734
91
35
695
396
290
214
153
932
992
189
615
727
816
466
82
128
220
32
995
156
535
448
460
171
76
831
497
578
290
44
644
426
771
789
413
810
631
793
501
731
641
516
993
699
906
215
429
529
295
273
809
753
390
650
183
547
900
353
358
366
466
642
569
912
940
784
147
364
559
InsertionSort - 0 steps
386
320
233
885
435
466
491
550
277
953
381
646
211
86
425
338
887
961
486
628
895
738
720
755
962
666
644
757
717
635
148
449
557
560
839
280
880
654
101
432
317
31
168
22
228
822
461
698
211
498
649
656
198
403
343
639
217
189
15
499
29
336
442
19
724
348
131
533
880
27
707
24
287
214
802
702
301
94
851
88
728
405
582
225
14
869
339
397
865
39
892
899
488
886
294
524
104
154
666
410
ShellSort - 0 steps
552
362
393
477
275
634
330
728
685
158
393
646
977
566
803
894
525
489
15
283
937
500
618
958
93
253
738
820
515
245
765
860
507
77
850
591
36
404
78
570
198
268
285
560
890
811
810
799
516
385
227
417
260
855
603
181
227
568
770
155
195
114
259
143
3
495
132
786
563
630
132
71
517
738
909
302
709
596
297
411
12
672
910
425
570
999
506
970
920
633
596
75
547
311
185
662
6
944
781
628
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