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.

721
45
632
912
394
682
534
244
153
809
179
70
993
599
985
289
608
758
212
491
2
885
661
879
888
110
101
853
487
42
571
309
96
769
146
208
459
510
884
1000
211
430
303
351
680
571
675
188
469
212
734
662
377
419
509
71
942
385
848
903
696
264
559
392
34
799
486
811
298
203
604
136
17
821
701
844
37
423
258
698
784
477
837
682
940
187
692
545
574
800
390
51
947
4
635
869
444
950
778
365
BubbleSort - 0 steps
658
812
716
35
298
589
651
383
927
895
865
709
665
396
505
906
95
575
412
864
792
324
506
372
349
652
913
336
450
619
267
44
863
38
858
226
112
91
906
493
569
257
756
62
621
880
590
283
623
860
893
124
863
285
527
185
951
672
326
52
384
889
312
201
163
789
79
911
56
481
763
780
438
218
76
682
294
390
599
433
207
554
368
120
768
147
357
594
414
835
278
705
96
140
226
9
838
390
37
968
InsertionSort - 0 steps
385
745
424
43
494
491
479
764
260
366
730
998
910
756
276
699
529
736
220
402
937
66
685
51
444
852
604
96
313
604
301
945
584
200
949
1
515
62
971
379
160
171
918
48
500
498
272
457
867
985
576
357
380
504
800
237
404
954
838
287
175
111
758
848
229
742
177
877
696
55
909
841
949
736
770
804
103
606
652
900
386
599
93
274
683
399
858
323
743
758
338
174
340
182
289
423
687
519
738
577
ShellSort - 0 steps
709
841
103
477
520
873
627
89
185
717
207
521
578
291
85
41
2
539
503
138
930
618
592
560
894
96
76
480
765
130
455
821
677
574
253
639
586
127
403
445
224
525
76
15
270
177
153
871
439
738
963
558
274
856
244
296
401
189
536
156
538
192
959
364
72
888
352
813
571
189
237
332
815
627
130
985
154
597
427
455
697
702
304
27
566
593
285
175
1
49
306
574
905
332
439
63
820
181
284
108
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