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.

202
873
891
657
648
368
209
427
693
293
80
903
172
575
715
980
756
748
973
373
480
279
958
929
90
391
327
526
343
927
69
358
545
45
751
547
215
627
609
682
203
984
111
448
683
380
404
446
866
650
528
157
380
797
76
884
714
258
503
296
411
873
445
550
221
540
694
497
86
589
439
259
67
923
461
695
37
726
919
155
906
866
630
122
152
786
37
308
823
607
779
518
426
663
383
661
347
130
932
662
BubbleSort - 0 steps
974
83
237
978
979
138
400
755
756
473
591
318
914
638
839
565
72
313
802
711
510
358
107
979
845
674
311
699
515
594
362
472
361
456
985
480
328
186
573
933
85
137
389
94
385
967
203
81
5
42
433
501
818
646
33
134
615
821
697
870
37
680
145
359
322
957
263
477
281
861
309
627
524
226
938
291
200
793
826
292
123
665
61
842
37
81
156
82
933
773
390
798
166
912
93
399
806
237
331
926
InsertionSort - 0 steps
702
756
332
2
51
666
284
206
628
244
372
591
531
563
198
990
80
55
543
388
309
658
592
755
114
793
155
219
209
872
203
910
54
203
835
134
272
649
212
922
442
353
297
911
988
343
239
487
448
243
522
500
136
11
770
624
34
860
545
150
981
858
40
182
893
188
453
103
356
500
601
292
11
908
133
718
518
143
512
618
965
193
558
784
997
309
15
49
966
122
480
360
928
603
388
470
703
821
497
289
ShellSort - 0 steps
115
454
167
244
110
828
883
336
665
985
39
880
758
942
619
388
762
709
463
770
427
296
480
115
927
368
462
285
275
612
648
32
217
108
461
358
799
602
740
120
181
47
103
937
908
467
874
522
85
391
86
227
419
549
319
938
550
958
35
404
387
994
798
138
695
155
998
423
578
424
936
971
99
782
97
846
464
914
726
588
857
939
679
678
989
339
838
223
851
294
740
174
866
988
193
891
288
796
750
735
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