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.

793
38
3
305
251
831
523
109
899
340
343
956
202
112
364
278
763
420
999
484
535
292
72
146
172
501
518
109
732
390
718
38
71
788
933
73
595
511
309
330
703
404
895
367
304
948
919
499
737
108
715
946
730
653
642
414
279
730
653
977
361
154
136
70
396
962
249
594
686
148
83
280
350
162
943
307
250
213
645
423
962
27
933
844
925
853
940
441
796
313
689
644
936
883
270
380
624
845
207
391
BubbleSort - 0 steps
926
993
408
520
555
857
960
189
134
430
53
729
254
456
242
223
614
410
642
703
333
900
443
687
184
182
197
579
944
821
394
588
740
673
634
972
683
373
467
728
737
135
971
730
979
761
318
762
668
585
212
178
501
884
652
540
36
381
877
590
877
824
236
175
330
849
45
683
955
514
775
747
356
193
90
339
373
211
37
366
625
924
378
109
969
624
732
444
540
8
874
272
916
864
936
99
434
233
869
514
InsertionSort - 0 steps
318
905
825
288
429
105
157
409
254
728
698
21
508
310
473
675
838
361
771
817
139
64
165
333
578
291
858
510
474
522
46
450
622
111
151
932
855
537
43
718
328
342
7
366
362
629
8
693
52
336
843
839
270
470
478
517
355
384
404
412
516
14
638
641
806
489
157
920
630
667
684
740
103
916
799
828
350
236
299
993
215
921
802
615
610
893
208
829
560
998
993
104
765
860
198
368
349
82
639
217
ShellSort - 0 steps
827
949
9
785
879
231
94
374
679
722
350
907
713
839
586
416
227
542
438
663
656
45
453
46
807
135
762
34
366
594
997
293
522
601
775
472
805
216
20
370
691
223
533
673
548
228
888
729
433
721
679
151
920
561
710
731
476
638
599
63
310
85
724
34
986
928
528
682
774
884
967
220
500
666
848
689
838
67
834
21
478
138
796
588
817
465
704
403
463
1000
685
424
773
503
801
27
819
392
705
227
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