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.

619
326
22
108
987
884
275
913
138
925
825
244
20
827
357
611
273
784
640
563
8
668
394
983
823
154
975
754
268
516
86
539
611
597
965
817
735
328
537
960
669
257
307
380
138
862
529
730
623
848
448
595
786
170
652
868
411
453
648
2
985
984
271
955
278
371
692
745
125
728
502
279
577
170
158
944
750
69
973
851
846
211
651
810
561
511
878
779
463
17
733
575
770
693
766
328
345
586
99
879
BubbleSort - 0 steps
862
931
987
575
318
960
460
205
162
12
700
301
196
176
860
806
134
600
639
205
689
272
757
740
868
948
751
625
318
431
396
606
175
542
929
816
299
769
391
438
288
917
113
404
380
410
324
560
199
304
889
676
927
337
84
96
633
632
8
108
612
705
944
634
885
698
604
295
959
820
395
536
49
444
889
163
184
262
860
446
712
149
738
735
542
746
416
282
432
314
490
729
765
192
239
133
245
562
2
823
InsertionSort - 0 steps
352
417
339
50
186
994
964
933
257
929
420
444
901
546
191
521
232
254
496
284
183
489
101
97
768
684
739
88
21
236
81
616
399
246
860
342
81
3
910
862
448
847
707
432
137
993
647
706
582
565
983
52
964
269
769
52
535
366
660
768
193
27
979
820
345
442
374
581
237
885
834
12
145
739
182
933
872
471
615
188
222
855
435
246
740
385
365
607
228
449
642
407
964
627
12
16
721
535
918
637
ShellSort - 0 steps
123
756
143
163
310
667
257
589
925
871
489
945
151
7
430
290
839
525
920
511
618
282
94
237
634
982
683
352
6
859
742
98
687
975
413
264
274
10
600
895
343
68
896
47
77
494
652
968
108
152
153
165
395
265
561
634
461
747
5
289
601
481
660
674
414
443
385
536
445
304
120
349
514
486
534
238
68
582
963
580
929
649
960
315
558
620
825
265
865
650
730
607
304
475
809
113
906
541
143
755
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