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.

725
454
784
324
847
583
598
653
884
964
970
501
179
620
693
379
54
345
907
871
152
319
551
500
138
423
823
866
522
894
168
877
393
625
693
741
885
352
827
183
534
122
167
844
695
663
725
295
182
150
430
682
230
901
12
224
514
236
118
905
246
302
773
361
43
709
649
6
202
799
720
255
409
125
400
493
938
787
509
736
730
2
475
168
837
877
201
966
211
166
24
503
811
635
471
134
238
819
501
948
BubbleSort - 0 steps
236
142
980
966
498
545
167
758
464
878
753
108
892
340
394
566
703
17
661
619
720
40
699
604
26
373
378
581
148
609
560
391
806
732
614
352
499
121
740
733
68
715
584
351
933
84
663
994
722
883
120
12
420
900
553
140
853
796
348
826
761
517
620
81
290
687
907
241
317
615
987
788
916
307
147
159
506
689
523
875
501
850
276
617
167
341
984
867
488
959
508
696
902
961
375
238
2
254
698
148
InsertionSort - 0 steps
279
779
180
473
684
892
888
103
91
691
153
801
651
833
553
39
639
634
38
830
383
683
286
177
672
263
752
230
940
730
872
830
302
904
523
937
664
813
730
795
598
377
972
237
192
51
884
108
656
28
649
324
142
865
223
75
479
179
72
284
885
425
451
461
950
472
605
75
906
661
778
280
281
451
356
864
386
6
501
585
2
797
486
312
450
415
860
238
813
118
899
252
91
582
579
165
159
893
455
663
ShellSort - 0 steps
354
534
764
548
410
656
867
949
335
136
425
981
641
40
505
785
247
937
212
192
430
166
59
883
611
393
419
644
268
3
662
657
273
518
577
658
549
497
624
999
597
181
575
512
677
826
645
695
298
631
686
313
316
838
514
501
516
408
591
692
509
969
840
127
947
620
453
692
765
462
836
281
334
828
701
691
279
45
262
893
114
177
511
550
466
971
329
277
892
919
381
999
26
704
883
390
777
706
491
535
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