∑GateAndNet
PlannerSign Up
INTERMEDIATEFrequently Tested 4 min read 5 views

Non-Comparison Linear Sorting (Counting, Radix, Bucket) & O(n) 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 Ω(nlog⁡n)\Omega(n \log n)

The Ω(nlog⁡n)\Omega(n \log n) lower bound (n!≤2h  ⟹  h≥log⁡2(n!)=Θ(nlog⁡n)n! \le 2^h \implies h \ge \log_2(n!) = \Theta(n \log n)) applies only to comparison-based decision trees.

  1. Counting Sort (Θ(n+k)\Theta(n + k) time & space): Counts frequencies in C[0..k]C[0..k], computes prefix sums C[i]+=C[i−1]C[i] += C[i-1], and iterates the input array right-to-left (j=n−1j = n-1 down to 00) to preserve stability!
  2. LSD Radix Sort (Θ(d(n+k))\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)[0, 1), distributes into nn buckets, sorts each bucket via Insertion Sort: E[T(n)]=Θ(n)\mathbb{E}[T(n)] = \Theta(n), worst-case Θ(n2)\Theta(n^2) if all keys collide in 1 bucket.

2. Finding the kk-th Smallest Element (Order Statistics)

  • Randomized QuickSelect: Recurses on only ONE side of the pivot: Tavg(n)=T(n/2)+Θ(n)=Θ(n)T_{\text{avg}}(n) = T(n/2) + \Theta(n) = \Theta(n), worst-case Θ(n2)\Theta(n^2).
  • Deterministic Median-of-Medians (BFPRT, Groups of 55): Partition into ⌈n/5⌉\lceil n/5 \rceil groups of 55, find median of each group, recursively find pivot p=p = median of those medians. At least 3n/10−63n/10 - 6 elements are ≤p\le p and ≥p\ge p, so worst-case recurrence is:
T(n)≤T ⁣(n5)+T ⁣(7n10+6)+Θ(n)=Θ(n)T(n) \le T\!\left(\frac{n}{5}\right) + T\!\left(\frac{7n}{10} + 6\right) + \Theta(n) = \Theta(n)

(Why groups of 55 and NOT 33? With groups of 33, T(n)=T(n/3)+T(2n/3)+cn=Θ(nlog⁡n)T(n) = T(n/3) + T(2n/3) + cn = \Theta(n \log n) because 13+23=1\frac{1}{3} + \frac{2}{3} = 1, whereas 15+710=910<1\frac{1}{5} + \frac{7}{10} = \frac{9}{10} < 1!)

Key Formulas Summary
Radix Sort Optimal Base Selection for n Integers in [0, n^c - 1]
T(n)=Θ ⁣(d(n+b))=Θ ⁣(log⁡b(nc)⋅(n+b))→b=nΘ(c⋅n)=Θ(n)T(n) = \Theta\!\left(d(n + b)\right) = \Theta\!\left(\log_b(n^c) \cdot (n + b)\right) \xrightarrow{b = n} \Theta(c \cdot n) = \Theta(n)
Note: Choosing base b = n sorts n integers bounded by n^3 in O(3 * (n + n)) = Theta(n) deterministic time!
Simultaneous Min AND Max Optimal Tournament Comparisons
Total Comparisons={3n2−2n even3(n−1)2n odd\text{Total Comparisons} = \begin{cases} \frac{3n}{2} - 2 & n \text{ even} \\[4pt] \frac{3(n-1)}{2} & n \text{ odd} \end{cases}
Note: Compare elements in pairs first (n/2 comps), then compare winners with max and losers with min!
#Algorithms#Counting Sort#Radix Sort#Bucket Sort#QuickSelect#Median of Medians

Practice Questions on This Chapter (1)

Q1GATE CS 2021 Set-2MCQMEDIUM
+2M • -0.67

What is the worst-case time complexity to sort nn integers lying in the range [0,  n4−1][0, \; n^4 - 1] in the RAM model?

⚡ Click an option above to verify instantly
Previous TopicFamous Sorting Algorithms (Animated): Bubble, Selection, Insertion, Merge, Quick & Heap SortNext Topic Searching & String Pattern Matching (Animated): Binary, Interpolation, Rabin-Karp & KMP