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.

867
229
9
976
190
170
725
691
64
853
275
816
533
512
271
543
230
150
913
864
585
167
92
171
856
781
614
93
934
454
326
882
367
895
156
186
505
395
714
575
613
480
389
252
960
467
928
170
751
921
855
543
443
606
715
501
528
100
243
167
491
318
671
241
663
384
618
519
584
166
922
96
115
805
537
332
41
849
151
235
75
943
188
410
418
773
81
285
876
231
206
766
260
308
767
280
96
279
257
214
BubbleSort - 0 steps
251
675
81
912
30
680
760
940
308
756
78
369
346
512
192
225
255
668
44
796
836
282
300
494
805
398
727
36
357
108
941
36
99
339
745
274
493
551
568
899
812
325
243
486
792
59
868
4
434
61
453
391
827
701
108
802
300
488
92
584
988
699
431
820
335
120
634
329
673
126
441
623
318
822
846
259
758
726
668
462
151
644
803
968
443
590
814
627
302
522
491
266
136
243
977
188
517
673
342
830
InsertionSort - 0 steps
450
663
498
990
955
870
18
341
253
597
586
427
834
187
786
555
376
870
866
892
5
745
335
266
909
567
917
758
515
551
273
471
435
904
270
637
421
171
347
50
501
463
312
637
263
654
708
80
395
262
649
228
338
942
64
389
275
293
353
882
943
369
67
699
745
834
755
40
647
818
259
861
483
520
724
817
423
18
796
616
105
158
480
280
955
13
733
41
191
10
181
488
216
732
695
232
154
164
286
369
ShellSort - 0 steps
432
841
562
997
416
435
30
737
760
210
439
309
958
19
952
814
384
228
285
470
261
260
26
896
249
148
123
24
639
666
127
153
587
831
943
576
642
722
210
119
922
689
115
530
136
176
92
765
315
676
446
375
526
837
807
824
540
13
744
977
878
113
440
113
875
663
410
226
623
360
546
360
754
986
489
284
288
715
371
975
969
575
509
994
487
39
600
674
353
106
986
804
479
298
673
722
314
551
825
20
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