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.

573
700
824
868
316
811
579
222
99
440
226
602
61
987
689
673
431
369
861
974
329
570
461
22
649
744
619
113
510
679
558
222
668
199
929
415
967
448
541
10
917
344
599
830
685
103
641
518
945
37
674
463
530
690
42
227
634
503
296
60
563
43
872
524
666
697
867
616
973
359
675
408
778
300
504
535
964
487
828
524
95
947
857
10
283
100
738
853
284
53
114
269
610
171
588
860
565
547
481
669
BubbleSort - 0 steps
498
125
857
313
108
285
403
964
690
582
463
250
355
881
296
151
83
877
206
998
407
769
381
994
309
157
235
638
519
884
383
22
894
142
950
299
438
541
595
206
43
119
28
384
566
975
853
461
775
635
87
941
33
114
535
624
794
747
455
337
401
551
773
41
348
471
229
177
522
291
22
24
773
913
435
246
562
986
894
716
509
525
630
487
681
437
937
161
616
909
573
457
64
433
187
168
876
915
498
859
InsertionSort - 0 steps
479
122
729
169
870
944
27
878
325
426
869
363
2
421
102
86
538
687
167
187
325
423
472
981
744
291
236
841
54
546
984
983
760
513
283
617
245
427
654
216
649
890
361
553
644
703
54
219
552
306
767
676
238
339
511
169
349
879
691
558
327
660
51
865
992
139
791
123
262
378
347
343
349
753
91
61
361
247
812
926
475
137
852
263
355
897
656
551
961
570
490
908
367
776
884
591
283
120
367
312
ShellSort - 0 steps
672
166
133
984
60
429
51
90
739
625
898
731
521
904
965
598
804
621
160
279
171
795
593
311
285
794
413
644
528
281
444
304
278
209
743
803
752
208
920
766
873
415
639
46
621
116
600
229
811
446
811
134
706
513
712
130
428
949
145
377
391
53
716
623
176
593
685
78
639
630
840
988
930
620
621
669
463
577
363
652
94
830
995
536
314
799
683
336
675
126
709
514
816
139
261
956
93
611
689
49
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