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.

653
440
410
819
43
146
671
214
221
630
866
87
715
1
187
154
261
272
206
606
25
848
412
998
311
380
839
191
462
958
915
200
418
438
382
46
732
191
392
468
861
313
960
947
592
474
293
433
648
44
284
621
736
99
504
859
985
619
518
765
125
219
905
16
95
547
222
818
57
483
578
8
716
781
34
32
538
969
721
619
609
93
376
865
515
824
428
778
350
499
22
45
975
904
284
417
226
636
934
405
BubbleSort - 0 steps
247
775
217
6
672
526
246
837
778
966
930
962
41
243
633
60
179
174
447
25
917
735
191
876
171
58
384
840
870
369
828
167
208
688
233
924
607
751
342
812
586
179
93
685
166
718
913
89
767
585
427
296
994
136
290
403
186
467
113
95
498
614
112
577
825
894
682
815
574
780
823
91
886
441
103
632
403
397
248
61
741
330
708
141
157
719
660
505
809
124
52
9
966
469
994
122
418
288
503
34
InsertionSort - 0 steps
472
330
170
108
617
331
667
846
14
482
913
865
196
268
495
337
643
903
571
264
773
532
405
192
676
340
725
120
850
778
650
179
463
672
851
204
351
423
875
296
408
482
848
848
222
356
468
844
821
278
703
557
172
850
112
939
722
849
46
293
936
548
24
135
630
656
438
471
62
597
315
136
639
617
634
852
482
158
497
579
418
682
170
312
826
757
829
548
916
448
61
297
224
800
582
907
413
975
734
205
ShellSort - 0 steps
993
650
610
989
980
661
353
916
946
822
775
798
942
188
123
616
745
733
423
93
920
521
142
483
868
644
180
127
70
653
21
769
632
826
147
436
996
475
703
115
744
763
303
348
508
208
171
406
876
737
856
830
179
407
514
533
748
633
205
489
588
435
427
193
192
238
424
66
71
897
336
196
165
798
211
559
159
661
721
453
281
213
115
973
385
814
644
88
213
999
384
865
127
707
475
49
171
388
993
174
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