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.

386
944
708
12
84
169
35
327
621
442
243
143
400
121
654
402
267
259
812
437
107
192
940
606
170
222
915
447
422
898
777
744
944
3
587
677
179
831
119
732
516
595
108
354
159
37
926
900
557
390
129
414
863
78
995
591
809
772
85
137
515
652
123
749
456
126
513
590
440
780
627
185
893
569
517
599
27
479
689
731
608
342
671
444
576
190
737
677
530
914
10
659
228
243
611
772
583
675
562
68
BubbleSort - 0 steps
932
119
440
914
610
17
806
124
136
581
698
70
160
321
488
56
172
143
315
227
493
325
888
785
610
538
914
475
763
161
18
798
397
137
915
202
109
385
228
454
3
428
730
937
124
628
36
414
239
651
425
504
301
966
421
524
592
514
830
623
898
2
836
903
114
950
430
376
861
690
816
762
14
441
292
229
702
219
381
739
499
36
219
657
505
812
484
309
518
391
636
706
735
179
932
930
521
248
886
820
InsertionSort - 0 steps
104
932
863
753
508
330
99
985
605
481
314
816
549
824
538
107
980
346
166
809
776
485
823
755
594
8
178
312
226
577
18
849
802
335
479
563
555
462
988
638
252
512
1
776
498
895
875
621
827
359
207
57
741
424
231
902
888
912
705
726
590
449
516
966
924
201
960
104
927
974
700
152
15
882
159
425
39
898
357
390
272
777
844
517
695
909
72
593
39
415
744
612
200
400
653
608
88
783
288
411
ShellSort - 0 steps
810
909
678
653
743
657
718
450
66
471
219
952
455
587
59
22
177
437
877
666
231
942
310
51
918
479
529
990
860
613
542
499
26
131
923
658
177
492
428
197
277
929
722
730
729
965
173
453
397
531
322
704
601
633
338
615
539
867
37
404
962
979
285
83
782
619
203
874
726
745
492
10
604
129
294
989
914
423
2
279
979
280
342
1000
163
133
362
830
999
756
39
708
962
324
186
278
577
951
304
147
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