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.

730
159
443
586
766
76
313
148
916
268
201
945
343
823
228
826
776
362
850
823
532
744
91
678
527
990
876
406
477
450
501
148
143
522
178
368
144
627
109
743
119
29
521
303
537
467
769
974
547
907
830
987
92
699
84
377
543
939
435
872
755
290
765
681
996
620
556
637
167
423
771
109
727
146
827
305
406
422
27
838
683
805
85
908
990
113
970
618
125
952
851
659
845
323
281
976
10
488
63
622
BubbleSort - 0 steps
67
95
917
5
436
499
47
125
127
686
95
908
366
25
589
508
442
641
477
768
358
324
241
3
780
772
2
766
448
42
503
209
964
798
303
248
168
385
451
546
524
399
50
557
678
743
125
116
98
706
887
482
691
699
180
27
211
398
611
453
794
18
956
733
56
158
956
783
110
44
900
595
391
486
702
571
380
199
151
910
867
240
770
518
720
34
388
492
487
221
736
939
518
308
979
727
779
864
335
733
InsertionSort - 0 steps
397
663
407
289
821
821
47
356
532
805
803
262
904
153
387
924
400
340
953
828
925
620
41
791
205
946
699
942
421
387
252
33
894
906
757
945
773
579
569
207
461
354
817
198
556
173
272
455
236
110
339
402
994
439
250
883
34
943
262
787
830
127
341
100
284
429
193
566
408
847
963
814
146
885
31
561
244
293
410
893
804
397
512
80
33
286
318
779
4
763
501
340
303
808
539
542
171
816
856
156
ShellSort - 0 steps
994
91
155
209
793
421
828
372
414
476
641
862
860
494
699
996
388
931
799
587
654
729
541
657
646
690
535
534
670
701
351
366
906
360
837
73
204
273
192
469
120
510
678
443
497
278
722
505
389
484
483
852
422
773
807
888
680
872
894
588
843
565
268
5
615
198
986
299
259
647
942
446
353
607
508
560
647
583
940
298
989
929
5
357
204
990
634
35
710
291
48
985
412
966
431
710
240
906
925
461
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