+1 vote
61 views
Show how to sort $n$ integers in the range $0$ to $n^3-1$ in $O(n)$ time.
| 61 views

Assume $n$ keys sorts integers $\epsilon \left \{ 0,1,....(k-1) \right \}$

Then according to counting sort , for $k$ integers can sort $O(n)$ time.

Now, here is the  proof by an algorithm:

Say, $L=$ Array of $k$ empty lists for $j$ in $range\left ( n \right )$.

$L\left [ key\left ( A\left [ j \right ] \right ) \right ],append\left ( A\left [ j \right ] \right )$

for $i$ in $range\left [ k \right ]:$

$Output.extend\left ( L\left [ i \right ] \right )$

This algorithm has time complexity $O\left ( n+k \right )$.........$(i)$

Now, using counting sort imagine each integer has base $b.$

Then, $digit=d=\log _{b}k+1$

Now, according to counting sort expression $(i)$ will be $O\left ( n+b \right )$

Then total time$=O\left ( \left ( n+b \right ).d \right )$

$=O\left ( \left ( n+b \right ).\log _{b}k \right )$

$=O\left ( \left ( n \right ).\log _{n}k \right )$ when $b\simeq n$

$=O\left ( 3n \right )=O\left ( n \right )$ [when $k=n^{3}$ a polynomial function]

Source:MIT Lecture

by Veteran (119k points)