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.

475
117
250
470
446
254
52
532
811
847
508
573
114
563
746
669
645
907
971
397
170
445
196
230
151
702
228
385
441
428
348
422
384
85
85
268
148
102
217
821
91
558
739
257
111
508
372
465
766
220
57
137
663
620
516
404
175
415
901
164
117
587
645
162
511
760
566
515
620
108
885
228
910
737
327
241
523
727
861
924
418
380
750
570
651
897
75
771
252
601
659
603
271
156
616
298
616
804
199
394
BubbleSort - 0 steps
172
857
442
103
331
759
100
67
656
789
672
355
123
961
672
45
212
984
362
183
337
280
548
891
270
5
824
162
902
24
244
146
952
343
711
877
54
844
556
587
877
592
455
505
964
553
863
980
571
895
867
12
376
191
689
911
617
127
552
615
516
120
599
717
431
773
188
373
347
498
727
482
439
415
493
433
897
294
418
484
694
317
686
465
45
762
774
811
842
83
19
375
865
667
28
476
424
674
42
543
InsertionSort - 0 steps
116
104
127
730
884
525
736
680
393
804
577
662
740
418
356
226
922
122
277
950
190
693
874
803
625
708
806
434
391
772
971
469
53
76
32
576
207
323
826
166
843
135
697
961
446
62
630
741
714
815
751
717
227
320
4
595
610
590
30
537
966
190
848
40
929
393
127
844
647
102
344
850
883
977
62
434
745
794
294
497
99
159
839
405
176
582
364
181
399
709
78
129
795
711
431
915
387
287
225
839
ShellSort - 0 steps
200
649
617
417
299
172
432
714
554
566
651
887
46
416
795
217
153
210
661
880
219
271
947
887
497
860
930
864
146
100
633
882
600
280
149
963
775
792
684
246
108
659
428
861
437
642
641
545
716
78
366
579
492
508
4
342
253
349
419
462
31
788
448
798
652
537
808
432
313
241
945
267
981
103
446
190
131
432
997
664
178
192
167
285
720
716
250
180
135
219
490
770
354
789
387
178
898
410
769
432
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