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.

933
752
815
687
397
635
787
745
970
574
452
891
861
609
620
514
815
476
266
721
352
730
792
655
417
267
761
974
373
458
33
77
247
74
834
616
808
2
655
222
742
196
689
574
278
355
595
621
665
310
602
269
778
892
383
176
129
366
681
229
314
400
391
428
358
419
671
313
581
222
788
269
909
571
219
520
805
547
798
390
774
45
220
162
112
393
873
951
316
782
50
796
376
911
717
698
178
26
398
724
BubbleSort - 0 steps
949
275
654
60
678
870
375
854
565
798
942
353
612
400
858
989
251
62
90
735
481
37
974
526
8
522
661
321
602
789
854
564
946
171
164
534
991
145
236
699
305
324
413
640
507
653
808
164
401
955
659
547
4
758
156
59
816
8
63
478
857
774
626
666
797
913
775
76
525
622
856
420
954
16
741
286
178
620
565
571
579
288
964
405
177
58
184
908
665
677
277
508
97
317
735
476
655
634
839
32
InsertionSort - 0 steps
576
573
515
865
500
959
537
901
196
64
358
177
580
135
933
48
886
711
816
694
742
381
173
133
136
608
197
288
459
613
989
259
152
234
240
686
789
68
74
272
689
852
408
1
140
696
69
232
546
680
26
861
374
918
813
278
353
79
902
33
544
396
455
332
552
238
242
969
946
794
264
256
286
816
292
143
705
528
837
120
22
533
625
935
639
166
174
899
161
923
948
650
166
3
819
781
612
967
728
264
ShellSort - 0 steps
185
480
22
600
466
395
202
605
814
974
921
206
70
437
991
477
660
733
253
916
536
765
499
525
157
703
299
146
480
114
350
825
16
839
419
819
138
209
993
381
244
982
868
429
577
39
937
635
176
902
462
117
694
329
494
175
328
604
388
48
529
999
989
857
280
371
793
181
781
573
222
235
722
615
192
310
597
769
551
155
510
254
721
649
373
523
865
903
403
897
522
854
278
683
667
287
782
297
930
705
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