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.

785
186
103
754
475
691
789
810
59
574
533
878
563
774
205
883
101
843
856
354
246
925
353
661
536
397
414
624
222
892
757
303
395
889
247
935
16
866
606
519
654
702
914
116
315
258
238
861
382
543
553
407
826
153
96
791
59
524
196
455
868
820
799
117
498
97
835
598
7
636
341
671
726
450
154
854
358
377
163
752
931
20
802
483
309
929
839
848
584
435
725
861
223
682
903
76
156
746
122
603
BubbleSort - 0 steps
272
574
997
480
672
66
501
344
501
388
263
915
477
215
328
1000
162
917
96
773
64
215
546
877
835
144
880
566
726
813
523
712
444
228
347
956
403
762
44
838
322
317
918
787
276
44
456
734
933
254
63
977
36
206
248
127
537
79
981
121
348
975
574
604
278
273
984
112
581
127
891
872
629
598
729
653
726
437
569
609
169
160
915
22
863
309
612
253
424
945
288
501
568
374
236
754
396
493
283
94
InsertionSort - 0 steps
599
897
435
243
894
119
299
688
360
950
554
579
271
875
390
307
227
972
446
488
784
876
385
962
174
954
857
674
583
378
562
745
878
441
250
196
709
541
987
898
678
11
443
909
715
551
104
975
756
931
897
273
436
375
401
464
747
158
236
124
835
541
380
457
148
1000
468
223
428
633
884
450
176
936
218
974
957
382
678
686
468
401
400
425
206
575
546
870
248
245
610
818
881
67
237
518
493
343
313
301
ShellSort - 0 steps
145
711
451
447
197
122
595
124
105
466
928
15
394
818
978
20
299
394
746
640
284
675
784
866
671
952
718
561
359
379
502
616
563
366
618
153
267
226
700
235
261
763
32
684
635
617
748
477
838
170
2
81
218
303
833
460
635
1000
362
330
189
789
627
959
238
425
563
579
775
431
883
137
905
538
199
825
771
130
647
745
493
633
860
948
983
525
261
286
208
446
501
77
766
589
13
906
19
782
139
31
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