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.

2
381
80
459
925
81
720
818
141
969
696
400
797
292
973
656
119
357
388
182
97
527
20
738
104
170
446
773
713
403
735
665
825
918
280
345
680
351
176
757
903
610
319
80
155
554
260
505
21
462
670
462
53
545
497
208
110
121
943
882
620
335
217
655
502
473
37
611
748
465
54
893
518
425
134
926
607
973
657
33
266
267
22
99
385
862
836
389
196
106
909
237
790
377
405
760
228
632
167
356
BubbleSort - 0 steps
925
313
77
34
125
557
575
573
962
378
240
641
5
703
251
160
713
613
942
743
934
35
262
10
542
681
427
412
493
820
710
453
956
363
699
579
412
890
316
846
347
588
651
307
964
185
465
568
543
280
963
742
20
572
225
833
650
673
793
100
609
382
220
377
404
325
308
547
79
803
955
413
790
683
393
208
473
125
410
405
40
821
300
483
157
932
113
101
460
96
510
506
119
885
245
349
233
920
914
684
InsertionSort - 0 steps
724
594
135
609
304
889
679
233
561
883
876
909
601
52
510
261
715
614
588
979
766
889
333
15
289
666
19
582
293
856
170
531
507
421
811
247
949
386
618
806
712
561
134
766
910
5
817
988
385
19
568
511
835
670
579
705
94
399
724
216
113
26
217
336
742
459
171
866
629
682
685
494
677
284
71
55
304
677
674
420
830
883
582
382
297
432
917
295
211
821
407
577
336
794
686
939
566
295
997
574
ShellSort - 0 steps
467
161
511
489
899
967
132
999
698
142
28
687
113
783
854
683
129
38
133
206
92
951
607
914
614
930
535
867
47
264
184
490
696
555
707
227
809
131
607
440
994
935
494
789
375
876
64
226
67
754
620
49
786
713
280
994
777
317
788
380
872
777
597
419
895
108
731
427
356
259
318
138
484
405
310
72
370
455
9
604
583
12
334
106
167
845
973
21
344
916
636
49
557
979
740
175
559
638
425
181
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