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.

511
171
331
82
172
882
581
839
723
487
830
752
963
442
783
888
252
759
633
178
73
537
48
222
41
755
403
384
566
635
286
561
384
262
664
870
954
25
696
665
694
369
358
493
943
718
151
379
840
715
189
390
509
680
297
1000
66
255
782
240
314
519
471
708
864
429
387
174
643
696
469
186
245
994
99
55
719
415
8
780
777
761
977
291
30
291
71
467
908
722
222
999
421
896
354
206
504
451
288
883
BubbleSort - 0 steps
299
459
882
509
142
471
32
517
641
81
873
6
977
262
459
298
19
53
776
922
157
318
683
993
469
458
627
444
128
643
811
924
627
679
834
891
253
250
630
677
72
926
575
768
227
712
339
975
277
683
531
658
170
529
265
173
269
579
941
104
772
812
945
175
331
924
670
841
432
54
863
112
160
556
499
971
78
921
275
292
567
678
244
13
949
216
148
329
669
524
241
789
787
92
878
999
522
342
106
910
InsertionSort - 0 steps
796
401
619
74
366
411
11
138
960
353
928
404
204
659
777
765
929
916
121
205
636
567
845
256
954
681
374
304
260
24
341
667
774
643
390
409
261
920
383
314
881
92
698
495
708
267
618
102
636
196
474
849
247
903
503
494
423
533
734
896
144
407
674
155
820
917
835
458
819
453
847
235
254
541
490
645
708
695
81
417
573
151
949
114
600
716
919
595
935
683
204
787
382
244
611
107
208
108
49
301
ShellSort - 0 steps
811
77
305
614
526
217
233
2
403
204
458
559
596
998
22
948
820
814
804
475
740
88
766
999
372
997
384
724
160
815
468
503
531
504
425
101
241
24
203
663
354
179
711
971
925
419
655
958
726
848
450
828
247
260
341
380
603
417
652
423
934
294
203
666
459
513
460
866
849
761
65
639
262
935
888
591
612
601
968
158
546
50
966
115
668
272
871
228
949
259
667
900
580
603
409
476
774
310
227
133
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