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.

575
155
634
230
841
316
719
199
911
732
380
513
715
549
955
821
508
614
741
287
71
563
88
852
409
890
653
200
476
616
344
61
591
796
571
704
192
16
56
448
257
415
595
33
207
734
319
138
480
114
386
145
948
327
375
991
411
875
68
253
604
78
558
722
453
79
606
505
559
621
535
58
675
376
243
526
14
694
910
708
263
255
442
616
543
16
822
652
95
412
254
336
457
675
409
321
192
961
711
777
BubbleSort - 0 steps
594
683
966
594
65
464
617
28
811
191
229
260
411
452
500
51
225
176
816
265
263
747
440
349
53
793
979
211
322
441
624
161
837
169
542
689
124
736
802
500
271
613
501
875
115
34
828
351
821
850
604
277
475
894
794
168
799
379
530
222
902
843
49
485
836
228
853
39
146
370
516
193
285
670
382
649
799
346
183
993
229
434
678
310
799
795
632
673
739
243
258
993
210
686
296
128
327
736
277
857
InsertionSort - 0 steps
894
351
383
979
371
732
852
622
522
852
554
119
794
970
915
98
315
50
972
567
44
164
851
667
408
200
464
893
583
730
619
270
205
780
974
256
202
623
480
385
683
944
158
133
196
233
919
143
90
374
671
107
240
481
727
979
723
619
352
514
267
907
121
903
40
814
770
282
428
853
212
52
172
887
684
236
163
230
944
365
197
997
622
171
480
944
14
540
949
775
22
224
116
164
474
59
975
727
251
750
ShellSort - 0 steps
807
875
98
661
361
324
282
961
679
804
479
471
929
908
752
143
822
806
178
553
375
175
221
163
952
842
33
606
787
765
723
661
309
447
212
759
28
833
4
96
411
703
861
517
30
746
120
255
292
34
602
143
479
52
125
387
89
533
539
66
63
538
479
556
12
603
701
590
240
430
496
224
240
449
800
703
297
208
618
82
284
911
788
311
237
909
33
898
508
698
522
523
296
197
552
30
918
119
972
630
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