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.

912
947
249
658
818
682
795
966
794
801
77
255
550
229
63
924
676
866
84
581
979
761
300
731
210
914
164
700
4
525
86
413
420
923
183
195
393
55
828
852
579
579
679
383
958
587
338
268
539
812
795
155
655
681
169
186
456
338
406
733
567
124
51
606
627
258
266
829
70
888
819
240
534
443
41
354
945
101
487
546
493
411
978
656
395
276
38
284
989
948
347
21
648
947
477
263
516
586
715
272
BubbleSort - 0 steps
34
155
688
509
458
730
94
213
574
933
473
959
967
248
588
712
167
64
441
932
363
195
603
376
237
932
970
110
160
974
463
111
767
482
870
715
599
807
579
1000
524
966
492
653
598
505
168
328
353
852
65
313
316
455
144
862
284
281
157
852
931
165
641
353
655
859
33
352
976
858
4
166
349
697
551
600
115
133
288
809
329
15
133
959
740
176
558
406
569
227
926
644
375
133
221
117
500
947
17
922
InsertionSort - 0 steps
295
314
749
64
762
80
557
583
560
949
283
112
996
310
805
271
248
368
283
745
7
815
726
89
188
181
391
801
433
644
209
150
801
700
516
895
536
274
682
501
429
132
878
316
139
841
236
126
358
935
531
363
369
165
822
318
111
384
24
349
936
786
954
960
543
630
625
366
25
857
723
6
166
769
762
916
829
329
47
162
47
426
816
496
531
285
457
961
925
566
382
946
474
171
891
698
617
513
351
854
ShellSort - 0 steps
43
656
147
565
288
49
936
437
631
771
302
296
122
836
485
190
117
550
805
175
621
38
930
709
826
854
676
348
157
995
42
493
55
914
489
598
834
930
488
412
539
485
347
986
606
116
206
925
407
405
677
458
133
273
4
448
709
133
697
955
974
670
170
741
430
474
428
254
95
814
488
17
796
430
482
339
534
299
44
899
578
307
459
410
118
96
501
476
513
510
673
507
874
684
300
209
546
221
137
315
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