Median Of Three
1. **Stating the problem:** We analyze a variant of the Median-of-Five algorithm where the input of size $n$ is partitioned into $\frac{n}{3}$ blocks of size 3, instead of $\frac{n}{5}$ blocks of size 5. We want to:
a) Derive a recursive formula for its time complexity $T(n)$.
b) Check if $T(n) \in O(n)$ by solving the recursion.
2. **Step a: Deriving the recursive formula for $T(n)$**
- Partition the $n$ elements into $\frac{n}{3}$ groups of 3.
- Find the median of each group (cost $O(n)$ since each group has constant size 3).
- Recursively find the median of these medians (the pivot), which involves solving $T\left(\frac{n}{3}\right)$.
- Partition the original array around this pivot, costing $O(n)$.
- Recursively search the relevant partition which can be at most $\frac{2n}{3}$ in size (since median of medians guarantees a good split).
**Therefore, the recursion is:**
$$
T(n) = T\left(\frac{n}{3}\right) + T\left(\frac{2n}{3}\right) + cn
$$
where $cn$ accounts for the linear work of grouping, finding medians, and partitioning.
3. **Step b: Guess and check if $T(n) \in O(n)$**
- Assume $T(n) \leq an$ for some constant $a > 0$.
- Substitute into the recursion:
$$
T(n) \leq a\frac{n}{3} + a\frac{2n}{3} + cn = an + cn = n(a + c)
$$
- For the guess to hold, we require $a \geq a + c$, which is impossible since $c > 0$.
- This contradiction means $T(n)$ cannot be bounded above by a linear function.
**Conclusion:** The time complexity $T(n)$ satisfies the recursion $T(n) = T(\frac{n}{3}) + T(\frac{2n}{3}) + cn$ which does **not** simplify to $O(n)$. It grows faster than linear time, unlike the classic Median-of-Five algorithm.