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.

377
743
354
605
55
159
907
528
483
218
39
283
437
785
521
32
791
73
159
389
58
565
476
736
18
268
46
679
740
790
204
427
67
296
1000
157
273
474
800
273
186
593
489
471
50
426
311
728
212
485
253
607
206
864
188
785
875
3
874
839
877
174
679
496
864
111
19
889
117
224
745
702
931
454
73
105
258
804
45
394
522
998
69
481
170
875
890
178
438
4
714
855
338
551
244
513
365
559
959
529
BubbleSort - 0 steps
241
632
498
519
865
466
871
464
857
214
319
325
87
468
841
745
529
239
34
570
793
822
538
554
129
795
339
66
211
446
129
56
341
976
644
918
387
606
281
2
256
626
536
347
424
12
223
934
129
741
633
75
124
255
305
206
923
62
168
504
864
422
439
70
375
427
586
634
20
892
6
924
786
654
181
634
847
722
416
571
526
87
151
459
699
824
317
276
923
761
612
432
714
28
650
688
75
471
29
946
InsertionSort - 0 steps
476
34
111
289
706
675
660
694
625
414
65
200
12
773
816
131
554
133
62
900
378
917
659
221
589
379
59
940
211
647
161
255
910
934
702
151
683
464
612
300
448
490
753
106
218
76
892
566
200
638
730
573
996
875
84
403
360
668
60
301
767
388
765
437
423
864
97
749
968
162
987
163
228
300
613
478
92
919
881
852
742
433
199
703
886
196
232
178
968
572
684
302
577
25
497
622
753
45
707
996
ShellSort - 0 steps
887
176
669
437
468
501
520
58
247
424
271
137
410
129
817
932
208
252
879
561
585
171
415
807
308
596
898
131
736
17
253
312
441
999
495
48
209
643
88
21
382
825
374
895
216
388
287
640
879
126
327
809
98
317
240
467
132
168
273
33
714
65
708
449
465
414
71
885
905
553
314
612
768
798
159
268
105
108
140
665
618
863
226
508
298
544
529
184
356
123
315
426
91
464
390
498
408
981
821
540
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