Background
Using a simplified Quicksort algorithm where the first element of the array is assigned as the pivot we get the following pseudocode for the algorithm:
Quicksort($a$):
- (1) If length($a$) $> 1$, then
- 1.1) pivot $← a [1]$.
- 1.2) less $← a[a <$ pivot].
- 1.3) equals $← a[a ==$ pivot].
- 1.4) greater $← a[a >$ pivot].
- 1.5) $a ←$ concat(Quicksort(less), equals, Quicksort(greater)).
- (2) Output $a$.
It is clear to see that the total number of comparisons will follow the recurrence equation:
$$C(n) = C(n-1) + (n-1) \space \space \text{ where } \space C(2) = 1$$
From here we see that $C(n) = \Theta (n^2)$
Here is the context under which this came up:
Question
The above is clear to me, however, I have now read that if we use the median as a pivot (which can be identified in $\Theta (n)$ time), then we get the following recurrence inequality:
$$ C(n) \leq 2C ( \lceil n/2 \rceil -1) + Mn $$
And this gives the result that $C(n) = \Theta (n \log n)$.
How have these two results been derived (the recurrence inequality and time complexity for the updated algorithm)? The only difference is that we are computing the median each time. This is achieved in $\Theta (n)$ time each time that it is calculated but it still isn't entirely clear where this inequality comes from or why the new time complexity is $\Theta(n \log n)$ for the entire algorithm.
Here is the exact context under which it came up: