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.

653
655
946
679
565
618
626
732
10
196
826
577
351
88
65
814
933
76
486
677
434
101
433
321
742
800
816
609
93
273
180
538
337
929
898
458
405
201
427
351
295
500
14
282
758
980
905
746
755
954
34
98
421
738
798
865
996
615
732
845
45
490
873
222
764
239
824
426
988
635
865
398
837
656
329
877
921
674
726
953
751
281
696
722
170
352
484
159
875
345
79
771
835
721
512
956
718
433
280
506
BubbleSort - 0 steps
964
871
605
818
263
381
86
865
549
195
620
837
577
162
638
680
51
940
125
354
455
291
745
807
455
835
309
846
513
210
644
427
122
616
627
68
32
871
316
143
792
322
486
110
123
406
518
516
812
537
53
363
597
831
276
922
363
778
607
776
375
119
514
232
645
736
604
91
896
77
548
815
556
618
38
283
352
764
206
816
304
952
363
612
596
769
631
847
791
226
672
732
17
582
570
292
455
958
408
539
InsertionSort - 0 steps
650
13
605
168
75
545
340
888
888
113
927
297
657
678
91
298
131
943
441
117
666
313
327
504
658
593
181
16
657
130
804
45
562
889
12
277
827
784
595
16
190
789
164
250
127
854
985
787
653
180
115
363
425
102
694
144
146
12
241
330
922
874
186
753
697
439
757
523
531
878
709
442
173
122
67
188
131
778
269
651
644
686
429
360
420
1
620
562
751
536
819
513
596
432
222
929
832
282
526
659
ShellSort - 0 steps
278
863
787
600
761
44
736
636
863
411
54
477
347
520
596
42
27
319
65
866
680
514
55
847
426
22
54
213
558
991
907
473
741
786
470
274
460
870
996
893
40
681
384
759
663
248
551
57
236
296
285
61
942
191
909
623
249
562
597
973
733
179
382
566
293
355
725
461
627
653
507
455
252
268
832
837
841
371
813
791
164
354
260
657
865
809
61
263
884
255
890
355
569
255
568
417
664
90
22
974
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