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.

201
652
921
975
502
89
925
803
581
349
927
445
55
264
784
325
792
499
536
136
956
83
40
39
334
10
121
307
771
733
849
200
365
899
466
682
133
277
545
232
298
115
851
19
681
962
303
776
182
572
807
931
663
787
929
177
257
264
766
717
667
459
329
586
584
633
282
366
439
300
42
160
387
350
937
454
54
339
369
261
9
860
133
72
883
891
974
601
772
108
14
44
877
219
786
93
233
815
466
144
BubbleSort - 0 steps
454
759
861
761
4
375
66
646
109
402
252
419
646
841
889
148
440
860
668
483
992
423
210
464
914
978
824
791
330
992
763
494
984
90
555
248
236
509
986
663
91
497
606
697
536
507
827
1000
910
862
621
714
111
878
360
54
855
171
796
20
391
618
500
160
369
158
655
820
697
511
303
367
943
786
335
900
686
813
770
664
319
357
688
133
540
694
75
725
327
144
650
255
296
784
486
408
318
846
18
212
InsertionSort - 0 steps
247
357
671
396
641
121
517
296
71
913
841
80
423
779
700
946
140
224
594
468
850
611
402
604
569
468
586
366
256
212
815
672
740
504
341
616
357
398
364
92
353
474
990
791
995
653
55
869
872
903
6
496
497
262
143
699
629
868
364
907
259
695
707
150
78
15
172
317
513
264
829
364
73
248
443
367
556
262
81
227
100
761
287
171
657
967
875
943
632
744
890
837
676
784
380
542
858
114
503
775
ShellSort - 0 steps
311
782
164
600
444
393
867
230
260
76
976
945
772
952
140
465
884
348
603
783
917
569
584
942
197
944
203
476
513
775
981
645
452
851
400
541
179
3
292
348
918
73
425
289
564
983
759
969
217
976
599
477
537
268
723
962
301
914
246
354
283
671
4
839
834
569
26
880
915
306
598
529
395
680
687
796
517
953
244
403
568
250
894
887
614
88
592
71
113
735
415
909
800
554
826
567
547
622
251
119
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