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.

983
524
592
121
33
115
847
7
522
304
818
343
426
720
637
794
638
306
328
509
327
993
405
801
204
563
298
838
459
722
849
158
654
826
487
439
317
817
524
991
861
501
758
908
547
723
499
870
750
510
679
137
290
427
989
50
88
298
415
260
599
530
435
757
191
40
621
81
545
569
158
147
111
936
861
386
410
275
212
512
274
728
945
377
535
875
403
244
140
645
637
309
728
476
861
349
180
132
566
72
BubbleSort - 0 steps
615
179
430
628
537
644
948
792
787
343
791
213
859
660
94
496
457
357
496
623
896
831
974
16
975
202
659
737
694
612
779
822
14
173
286
369
685
752
920
230
753
206
534
927
964
177
821
285
524
538
38
435
487
235
648
476
910
459
136
859
260
950
692
308
155
854
783
378
579
430
3
554
742
836
811
759
845
881
673
59
366
120
655
570
397
80
211
89
568
215
254
228
797
874
457
827
541
277
167
233
InsertionSort - 0 steps
296
827
83
510
542
601
30
861
681
27
676
592
61
39
598
428
963
277
257
316
968
381
203
564
764
974
303
52
106
154
730
865
941
732
661
818
560
103
563
99
506
970
678
263
125
174
18
360
166
174
526
496
566
382
467
748
33
269
621
724
155
80
867
536
611
840
2
576
481
343
874
42
869
332
416
461
576
325
821
235
206
934
7
989
937
501
600
468
299
923
34
95
901
681
902
392
304
259
575
83
ShellSort - 0 steps
249
436
275
372
815
349
581
492
195
652
664
848
216
84
209
829
353
680
602
758
282
368
800
984
813
224
695
381
316
32
277
635
118
345
783
278
863
211
403
348
914
846
555
806
381
331
142
281
403
726
663
829
522
287
672
971
715
575
852
908
446
532
955
714
723
323
12
955
846
952
174
102
484
822
329
183
233
469
248
556
888
373
981
205
14
22
752
301
195
732
212
196
253
146
736
174
9
47
295
423
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