VISUALISERS · FOUNDATIONS
Sorting algorithm visualiser
Sorting answers which algorithm, and on what input?
COMPARISONS
0
Nothing compared yet. Take a step to start counting.
“Step” advances one comparison; “Run” plays out the rest of them. “Shuffle” deals the same ten values into a new random order, which is the middling case for both algorithms. Switching algorithm restarts at step zero, so the two counts are always measured over the same array.
Where the two cross over
A shuffled array is the only case the control above produces, and it is the one where the two algorithms come out close. The two figures below are fixed: the same ten values handed to insertion sort twice, once nearly sorted and once reversed, each shown at its last step.
Nearly sorted, insertion sort makes 10 comparisons and merge sort 21. Reversed, insertion sort makes 45 and merge sort 15. Merge sort's two counts, 21 and 15, sit close together; insertion sort's, 10 and 45, do not.
Nearly sorted
INSERTION SORT
10 comparisons
MERGE SORT
21 comparisons
Reversed
INSERTION SORT
45 comparisons
MERGE SORT
15 comparisons
Both figures end with the same ten bars in the same order. What differs is the number in the heading — what it cost to get there.
Want an explanation to go with the picture? Find a guided starting point →