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.

965
435
991
92
15
838
652
991
840
953
408
174
324
454
979
354
339
844
492
617
792
233
277
682
404
860
808
555
28
406
805
824
474
724
163
712
718
503
842
366
234
777
986
37
948
672
338
590
915
89
571
331
793
719
69
447
421
127
854
569
732
110
279
50
335
893
905
518
384
160
544
708
521
817
123
89
63
652
482
133
125
429
630
748
307
758
765
635
957
11
386
100
974
925
179
461
93
879
327
81
BubbleSort - 0 steps
523
7
402
618
136
103
169
628
145
386
390
543
885
896
437
676
453
979
808
246
251
910
950
903
616
76
699
668
267
658
959
122
714
261
274
817
553
775
503
546
219
187
108
631
549
563
107
467
392
508
914
329
853
947
239
259
983
409
498
270
379
60
813
626
629
943
654
821
116
827
878
723
442
959
921
312
303
416
776
213
759
380
891
239
42
699
207
45
399
389
420
203
283
856
174
452
688
640
414
844
InsertionSort - 0 steps
702
986
4
927
413
429
859
211
781
520
145
891
488
630
660
938
819
241
71
367
962
576
93
412
202
581
603
335
813
913
842
80
368
94
799
62
505
291
653
3
298
847
694
593
680
906
824
566
601
541
430
160
538
798
98
193
651
510
633
362
856
194
778
563
962
924
248
422
975
642
757
231
67
596
728
475
799
675
814
548
449
763
857
418
284
842
503
444
120
196
305
940
344
512
201
101
554
37
13
63
ShellSort - 0 steps
800
101
350
38
222
547
445
191
912
156
144
798
714
275
843
223
661
808
83
345
235
828
226
144
584
370
318
83
418
745
885
112
40
361
232
838
256
721
416
614
615
501
502
458
378
802
20
859
230
785
720
65
540
985
972
811
50
942
86
446
765
687
963
630
204
272
604
55
697
142
363
993
571
22
376
28
503
464
846
305
703
694
826
564
45
722
973
529
130
339
933
789
667
469
573
27
662
474
691
11
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