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.

924
928
269
315
707
384
415
847
217
110
759
260
371
498
787
617
20
587
939
601
968
898
858
820
499
663
439
715
239
711
804
529
252
435
271
258
224
822
621
237
577
980
900
68
460
172
511
847
241
409
223
266
818
236
218
402
491
196
978
221
75
457
586
442
422
333
493
357
371
534
633
217
843
753
695
608
159
876
499
200
925
989
914
994
498
309
52
565
642
434
50
453
892
71
900
734
467
806
138
834
BubbleSort - 0 steps
976
190
147
807
694
401
1000
112
282
671
745
752
296
394
478
464
752
571
714
875
339
191
59
353
370
971
852
602
524
872
630
879
377
868
90
633
152
314
595
521
696
224
640
150
396
25
894
933
287
895
26
123
920
104
476
146
340
114
565
187
501
670
653
213
807
386
388
952
323
187
132
33
638
147
490
32
108
781
971
180
862
305
493
947
84
110
532
960
9
373
415
359
565
974
743
481
266
660
751
155
InsertionSort - 0 steps
251
473
360
758
739
666
945
774
13
536
852
763
687
116
874
290
896
737
437
90
163
939
673
566
9
975
301
946
860
413
852
959
113
452
801
404
741
699
979
682
393
156
145
674
201
138
926
736
68
224
977
73
907
895
396
149
509
740
832
284
605
211
51
401
163
629
570
302
574
42
992
196
397
290
888
725
287
391
370
34
813
920
309
653
161
744
517
554
756
600
686
581
540
519
626
403
275
268
426
437
ShellSort - 0 steps
252
798
366
221
794
106
855
134
417
644
474
53
907
727
735
386
345
22
22
958
812
521
18
39
694
620
847
260
697
61
984
504
553
527
957
473
569
525
935
268
508
14
707
939
916
255
297
602
757
266
621
549
214
809
841
115
30
724
607
337
831
48
945
412
686
290
856
257
414
59
89
203
842
83
772
139
524
331
311
917
225
385
811
497
774
960
555
518
415
788
435
703
383
841
929
25
70
558
360
706
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