# Non-Comparison Linear Sorting (Counting, Radix, Bucket) & O(n) Order Statistics
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Algorithms
Canonical URL: https://gateandnet.com/exams/gate-cs/algorithms/counting-radix-bucket-sort-and-order-statistics

> Beating the Omega(n log n) decision-tree bound with key-indexed Counting Sort, LSD Radix Sort stability requirement, Bucket Sort expected O(n), and deterministic O(n) Median-of-Medians.

## 1. Why Counting, Radix & Bucket Sort Beat $\Omega(n \log n)$

The $\Omega(n \log n)$ lower bound ($n! \le 2^h \implies h \ge \log_2(n!) = \Theta(n \log n)$) applies **only to comparison-based decision trees**.
1. **Counting Sort ($\Theta(n + k)$ time & space):** Counts frequencies in $C[0..k]$, computes prefix sums $C[i] += C[i-1]$, and iterates the input array **right-to-left ($j = n-1$ down to $0$)** to preserve **stability**!
2. **LSD Radix Sort ($\Theta(d(n + k))$):** Sorts digit-by-digit from **Least Significant Digit (LSD)** to Most Significant Digit using a **STABLE** subroutine (Counting Sort). *(If the inner digit sort is unstable, `[21, 19]` after units pass `[19, 21]` could corrupt tie-order on tens!)*
3. **Bucket Sort:** Assumes uniform/smooth input over $[0, 1)$, distributes into $n$ buckets, sorts each bucket via Insertion Sort: $\mathbb{E}[T(n)] = \Theta(n)$, worst-case $\Theta(n^2)$ if all keys collide in 1 bucket.

## 2. Finding the $k$-th Smallest Element (Order Statistics)

- **Randomized QuickSelect:** Recurses on **only ONE side** of the pivot: $T_{\text{avg}}(n) = T(n/2) + \Theta(n) = \Theta(n)$, worst-case $\Theta(n^2)$.
- **Deterministic Median-of-Medians (BFPRT, Groups of $5$):** Partition into $\lceil n/5 \rceil$ groups of $5$, find median of each group, recursively find pivot $p =$ median of those medians. At least $3n/10 - 6$ elements are $\le p$ and $\ge p$, so worst-case recurrence is:

$$
T(n) \le T\!\left(\frac{n}{5}\right) + T\!\left(\frac{7n}{10} + 6\right) + \Theta(n) = \Theta(n)
$$

*(Why groups of $5$ and NOT $3$? With groups of $3$, $T(n) = T(n/3) + T(2n/3) + cn = \Theta(n \log n)$ because $\frac{1}{3} + \frac{2}{3} = 1$, whereas $\frac{1}{5} + \frac{7}{10} = \frac{9}{10} < 1$!)*

## Solved Practice Questions & PYQs

### [GATE CS 2021 Set-2 | MCQ]
What is the worst-case time complexity to sort $n$ integers lying in the range $[0, \; n^4 - 1]$ in the RAM model?
**Correct Answer:** B
**Solution:** Represent each integer in **base $b = n$**: every number in $[0, n^4 - 1]$ has at most $d = 4$ digits in base $n$! Running **Radix Sort** with Counting Sort per digit takes $\Theta(d(n + b)) = \Theta(4(n + n)) = \mathbf{\Theta(n)}$.
