Question about quicksort

65 Views Asked by At

It is known that the expected running time for quicksort is $\mathcal O(n \log n)$ if the pivot is chosen uniformly at random. In this case the running time $T(n) $satisfies, $$ T(n) \leq \mathcal O (n) + \frac{1}{n}\sum^{n-1}_{i=0}[T(i) + T(n-i)].$$

If the pivot is chosen according to some probability distribution over the elements of the array $p(i) = \alpha (1 - \frac{1}{(n+i)^2})$, where $\alpha = \sum^{n}_{k=1}(1 - \frac{1}{(n+k)^2})$, can the same bound be proven?

I know that the running time in this case would be $$ T(n) \leq \mathcal O (n) + \frac{1}{n}\sum^{n-1}_{i=0}p(i)[T(i) + T(n-i)].$$

I'm not sure if the previous bound can be obtained or not.

1

There are 1 best solutions below

0
On BEST ANSWER

The bound in this case should simply be $$ T(n) \leq \mathcal O (n) + \sum^{n-1}_{i=0}p(i)[T(i) + T(n-i)] $$ without the factor of $\frac1n$: we multiply by $\frac1n$ when we choose a uniformly random pivot, in which case $p(i) = \frac1n$ for each $i$.

Anyway, if $p(i)$ is within a constant factor of $\frac1n$, then the $\mathcal O(n\log n)$ result doesn't change. That's true for the suggested probability distribution: if $p(i) = \frac1{\alpha}(1 - \frac1{(n+i)^2})$, then $\frac1{\alpha}(1 - \frac1{n^2}) < p(i) \le \frac1{\alpha}(1 - \frac1{4n^2})$ for all $i$, so all the $p(i)$ are actually nearly equal, and get closer and closer for large $n$. (For example, when $n=10$, all the $p(i)$ are between $0.0996$ and $0.1003$.)

We can certainly say that $p(i) \le \frac2n$ for all $i$, no matter what $n$ is, which means that $T(n)$ for this modified quicksort is at most twice $T(n)$ for the normal quicksort: still $\mathcal O(n\log n)$. That's an exaggeration for an easy-to-prove bound, though; in practice, $p(i)$ is very close to $\frac1n$, and the bound hardly changes.