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.

272
719
955
489
199
847
471
646
639
502
578
779
385
942
644
416
293
918
598
347
762
636
271
146
135
148
284
180
639
408
275
551
41
309
795
494
155
611
567
168
73
127
497
157
865
249
38
318
615
290
438
852
410
907
620
383
857
704
595
123
74
881
372
248
857
26
435
395
736
168
579
196
635
171
452
483
948
609
108
703
648
570
529
145
881
559
666
28
121
472
822
752
131
134
948
736
218
750
93
467
BubbleSort - 0 steps
75
978
727
992
526
845
556
865
91
721
578
823
820
758
82
194
645
559
253
919
184
356
730
461
828
670
29
550
929
423
746
77
698
246
684
402
216
821
625
815
521
171
904
191
277
764
267
428
804
73
448
643
285
108
55
784
552
904
924
205
655
52
767
672
286
18
413
15
648
128
464
645
910
765
695
190
627
14
765
580
326
285
295
358
532
637
96
537
877
134
593
304
582
754
797
78
117
242
110
48
InsertionSort - 0 steps
544
81
750
155
340
227
779
361
113
453
940
858
247
459
862
426
668
142
977
242
198
115
649
313
684
182
308
468
283
477
136
871
414
624
334
146
201
23
565
458
652
531
852
844
988
996
948
704
466
742
112
875
467
533
768
994
189
848
973
274
600
546
649
721
569
121
167
415
772
163
573
421
19
908
989
316
312
482
494
439
790
257
765
30
308
41
938
671
907
137
803
910
910
322
298
242
811
187
428
431
ShellSort - 0 steps
846
717
624
500
299
411
174
439
907
324
349
390
424
976
143
478
138
960
862
266
719
693
141
354
308
69
557
889
589
386
551
285
888
651
762
327
943
508
51
68
531
248
537
727
842
892
732
969
834
322
279
658
432
486
700
820
666
707
391
163
109
644
404
537
826
54
918
554
412
729
720
880
178
847
954
893
943
451
343
322
427
425
581
412
477
359
308
682
668
658
41
586
763
311
892
559
319
121
221
914
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