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.

920
130
757
504
582
771
595
309
844
386
575
897
418
92
421
476
909
771
895
374
244
213
405
196
209
53
501
224
901
94
571
907
989
295
516
866
767
187
487
821
505
105
761
278
538
116
377
773
404
439
613
793
787
507
577
101
538
356
410
173
402
414
550
344
143
759
944
399
313
512
569
265
942
138
782
372
543
792
787
371
360
818
45
251
574
806
862
584
619
227
200
146
349
311
410
832
444
307
39
803
BubbleSort - 0 steps
923
466
583
584
354
349
452
921
364
785
475
688
872
586
34
100
24
762
194
955
90
149
580
866
207
863
99
914
997
446
307
721
307
709
78
391
739
661
853
265
665
206
60
332
202
942
695
873
145
412
248
941
845
500
455
849
372
235
498
59
131
293
964
243
677
76
335
673
999
558
318
712
376
997
841
400
77
787
381
893
325
483
482
662
3
333
794
479
997
55
954
797
828
571
337
38
748
176
637
232
InsertionSort - 0 steps
895
130
257
883
631
225
198
236
215
886
485
724
342
817
761
160
267
945
423
171
22
410
820
179
378
101
715
396
503
103
865
834
278
877
133
109
246
665
289
500
382
239
398
627
726
550
695
903
73
733
565
896
366
33
659
669
98
473
896
924
320
765
460
368
636
487
867
181
383
884
235
426
835
278
269
667
418
734
692
42
421
369
123
577
869
100
738
813
501
15
290
835
934
122
3
111
142
468
72
540
ShellSort - 0 steps
267
751
331
778
968
18
437
24
492
189
566
867
416
903
930
456
987
504
569
222
682
401
850
30
20
984
647
935
561
345
592
812
553
872
61
139
412
395
927
295
100
769
613
14
732
731
166
503
513
130
331
975
806
429
338
655
310
123
673
841
16
178
981
291
401
701
74
456
477
911
805
411
40
825
606
718
200
117
297
57
147
331
79
989
797
477
433
692
454
471
959
779
679
577
611
419
464
782
334
76
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