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.

536
886
627
488
448
598
4
911
227
299
235
801
827
983
927
431
107
315
168
716
50
54
703
149
446
37
905
540
28
103
638
515
122
366
22
585
298
567
988
372
575
669
87
391
530
416
906
530
844
145
639
381
985
457
770
459
586
381
82
464
231
430
182
789
935
775
356
586
904
309
9
126
350
634
167
792
734
315
220
832
770
410
16
149
89
885
467
575
436
425
720
180
7
841
865
206
659
516
367
918
BubbleSort - 0 steps
97
699
715
110
817
115
701
409
229
489
915
702
551
505
992
592
695
939
422
415
693
366
206
706
680
220
770
164
743
833
511
56
203
709
286
383
314
120
872
202
813
742
875
19
643
929
789
208
898
144
441
637
765
127
726
403
561
111
191
887
721
441
434
687
528
706
655
794
592
859
267
800
281
193
345
551
113
670
770
603
769
997
518
636
367
918
246
668
292
594
882
139
674
665
285
538
704
587
286
257
InsertionSort - 0 steps
488
270
312
732
912
138
498
489
639
230
381
963
75
555
972
512
201
613
984
82
288
149
170
235
179
850
446
613
34
159
184
841
978
755
76
41
103
176
991
513
676
39
859
486
487
858
673
275
822
782
714
596
449
817
562
923
798
502
948
858
41
294
61
752
619
398
12
219
431
204
618
395
284
881
437
372
395
565
364
601
648
961
593
375
677
948
487
692
827
289
845
711
504
613
200
651
257
593
22
685
ShellSort - 0 steps
248
946
157
817
509
828
116
492
573
476
146
299
119
847
89
974
871
818
420
197
989
428
295
232
629
526
511
289
83
755
48
68
85
585
982
392
280
392
60
932
483
245
887
526
58
513
865
521
221
671
579
660
926
161
670
580
769
492
457
741
900
436
552
927
623
838
565
427
322
527
967
164
711
871
291
619
351
81
635
746
956
586
19
181
77
482
452
628
882
814
467
773
305
469
358
443
541
427
753
260
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