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.

56
296
34
380
724
760
647
571
301
209
171
314
272
374
432
892
774
333
856
386
744
744
319
550
210
571
591
807
860
32
446
853
861
970
731
381
182
583
816
403
570
190
432
363
974
996
804
278
944
549
871
606
626
669
845
84
804
537
802
308
628
113
249
520
958
532
40
576
221
728
921
719
955
796
417
600
360
231
865
119
476
293
705
213
764
599
346
474
185
557
500
272
274
2
330
663
917
862
969
222
BubbleSort - 0 steps
724
897
206
778
111
934
443
829
373
731
456
964
846
446
7
599
528
136
852
228
977
438
236
452
906
726
173
766
158
979
982
610
606
803
693
778
111
738
794
315
333
388
552
894
319
850
980
625
68
763
83
615
73
920
378
656
341
319
992
509
355
155
719
613
714
297
254
869
446
418
501
791
56
214
735
752
800
938
888
421
971
251
553
771
665
366
351
186
89
918
616
711
886
683
525
691
329
56
496
520
InsertionSort - 0 steps
877
277
771
760
641
833
975
538
718
608
387
114
322
736
837
560
932
446
343
396
726
591
201
788
994
604
496
529
418
684
968
778
817
306
458
116
25
153
43
516
728
922
58
684
872
832
654
618
959
45
50
157
393
954
419
835
320
247
520
149
346
120
150
506
413
533
677
830
226
823
91
285
206
308
469
110
641
796
601
501
998
235
648
669
442
246
173
862
8
771
442
774
836
436
719
893
880
666
76
902
ShellSort - 0 steps
578
462
989
226
165
688
803
158
942
735
373
669
482
653
362
225
199
861
543
628
448
288
72
659
2
267
199
155
900
887
522
668
281
949
761
996
715
720
783
695
145
476
81
968
476
426
252
370
33
492
44
895
369
944
239
308
811
470
511
871
40
242
278
527
662
484
854
24
475
567
305
601
436
244
669
891
647
151
640
435
287
428
195
918
443
430
812
351
166
114
846
677
100
272
225
944
303
794
210
657
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