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.

781
10
268
560
381
790
642
481
212
830
390
209
611
118
401
965
931
882
48
453
794
765
137
629
879
251
972
920
611
570
681
591
454
500
936
148
273
634
586
806
869
879
522
579
663
800
48
856
952
789
643
497
306
465
13
531
1000
61
178
38
873
720
536
540
747
747
354
706
573
807
556
151
577
215
346
263
516
983
47
569
98
342
685
87
193
41
978
595
377
374
177
621
145
551
112
29
124
423
10
952
BubbleSort - 0 steps
863
604
195
264
817
49
340
239
52
955
199
808
439
428
259
678
524
680
501
846
339
885
76
160
115
711
344
571
151
406
498
582
552
301
409
915
151
884
116
898
517
64
301
691
207
904
240
87
312
969
888
497
458
849
149
678
139
969
792
331
211
951
598
168
213
460
436
915
505
344
680
327
199
133
873
630
769
392
934
805
712
589
881
157
299
287
438
265
364
357
569
587
725
696
427
118
847
433
219
775
InsertionSort - 0 steps
692
495
186
795
900
188
107
51
660
360
289
866
843
62
795
399
157
796
540
802
809
755
732
977
489
396
999
461
765
604
838
852
954
467
588
753
222
456
100
992
851
386
873
148
970
405
510
803
561
713
84
641
639
688
288
802
706
627
660
890
102
925
241
751
196
175
359
315
413
114
277
913
812
248
217
335
595
586
221
474
24
335
529
805
585
654
241
798
677
754
885
8
188
936
788
962
446
774
974
663
ShellSort - 0 steps
660
839
514
294
736
227
299
674
578
838
613
988
223
237
978
636
280
72
874
18
233
426
580
427
412
761
252
553
449
339
858
410
847
952
977
101
486
219
122
311
758
488
993
548
768
862
642
927
13
934
85
140
280
365
692
455
149
340
781
67
361
328
717
80
668
226
40
532
155
481
471
901
370
973
467
385
652
761
649
725
487
467
935
35
852
172
297
926
360
384
879
991
823
434
106
59
476
925
31
388
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