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.

590
525
363
328
735
928
366
459
168
384
493
35
548
946
877
20
270
882
345
192
104
193
292
483
240
247
992
567
51
319
954
684
333
647
722
119
307
852
685
71
92
459
850
517
311
440
441
28
814
209
373
389
871
977
812
811
763
30
342
886
295
300
362
650
312
851
449
996
138
553
716
222
553
428
432
575
937
713
328
79
134
23
223
256
72
77
914
394
550
10
653
543
691
974
682
139
522
352
227
539
BubbleSort - 0 steps
838
666
50
819
321
364
60
594
742
241
546
382
864
532
903
751
623
724
711
629
950
585
512
345
940
148
327
60
956
872
261
479
676
955
522
74
968
970
627
80
471
948
611
693
239
329
435
985
807
605
564
801
706
712
417
639
666
153
806
111
290
80
211
340
694
238
442
937
888
656
953
623
409
383
407
295
536
539
242
932
822
217
769
649
115
686
760
292
574
841
51
449
341
160
956
501
665
21
431
371
InsertionSort - 0 steps
62
274
27
126
864
505
373
349
713
659
597
502
769
258
703
36
848
776
674
942
703
288
537
549
616
615
664
776
391
531
446
261
785
115
97
768
180
401
397
976
553
694
642
421
578
861
899
841
408
92
375
780
126
964
643
105
223
594
158
164
624
593
299
565
853
719
899
570
877
448
869
861
903
749
325
74
886
770
51
432
682
985
955
88
825
162
102
21
371
721
983
871
75
352
502
782
491
319
302
623
ShellSort - 0 steps
358
117
103
507
35
921
670
667
661
378
81
103
734
124
613
705
669
540
181
2
941
426
540
416
471
89
842
351
973
301
443
547
579
96
868
106
199
753
155
44
163
716
340
304
804
741
486
203
232
729
647
892
469
313
684
940
326
406
835
607
516
426
254
163
90
381
494
620
194
845
739
173
939
965
983
851
664
340
800
564
712
401
924
519
173
228
11
860
368
811
39
918
16
344
415
857
764
222
46
36
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