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.

138
299
112
146
866
92
576
496
555
913
926
249
922
763
255
194
132
245
267
502
194
677
527
307
429
365
77
192
484
822
259
102
628
960
254
87
851
817
509
504
544
804
140
521
741
531
651
354
232
782
638
256
940
524
110
749
223
951
562
57
582
215
360
906
675
20
253
244
380
69
15
478
462
718
700
354
863
440
159
877
180
838
19
370
584
81
767
147
77
169
802
83
4
227
643
315
640
433
248
476
BubbleSort - 0 steps
815
255
529
692
42
588
610
16
650
941
307
25
693
71
550
903
903
71
755
37
807
124
515
300
842
788
618
68
289
774
619
393
421
77
233
228
512
842
43
940
790
368
249
340
374
487
609
509
957
841
36
772
679
785
972
70
748
537
963
4
686
666
9
768
544
942
385
692
582
675
761
757
117
309
169
599
309
940
616
948
448
951
946
16
779
444
113
512
158
730
717
327
601
92
409
516
707
815
581
784
InsertionSort - 0 steps
880
685
254
381
350
291
244
489
967
282
682
668
754
587
899
682
120
487
925
17
926
329
696
806
342
214
272
944
125
882
425
67
146
682
68
125
588
890
354
538
121
403
846
422
860
992
811
565
352
333
624
106
54
123
986
485
141
556
616
602
264
339
688
44
970
843
402
941
644
970
784
394
434
431
73
507
483
535
541
489
951
422
308
975
138
96
968
351
1000
739
794
741
914
509
459
335
53
126
692
6
ShellSort - 0 steps
975
972
852
292
241
20
132
301
479
429
932
723
284
261
629
998
193
343
329
714
311
267
660
660
492
425
361
636
225
363
57
581
557
709
784
329
392
228
921
208
263
274
174
579
187
944
71
116
996
440
80
640
434
251
555
135
619
25
289
676
655
512
807
681
980
543
845
709
586
1000
859
906
296
223
568
607
598
584
460
60
49
881
67
604
635
534
176
500
761
496
938
81
792
979
113
976
469
989
755
938
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