Quick sort and quick select are algorithms based on the partition algorithm. The partition algorithm produces two sublists ($\mathcal{A},\mathcal{B}$) and one pivot value ($\gamma$). Every element in $\mathcal{A}$ is less than or equal to $\gamma$. Every element in $\mathcal{B}$ is greater than $\gamma$. Note that $\mathcal{A}$ may contain elements equal to $\gamma$ but not $\gamma$ itself, and either $\mathcal{A}$ or $\mathcal{B}$ may be $\emptyset$. As a result, the partition function places $\gamma$ as the maximum element of $\mathcal{A}$. Quick sort uses this property to sort the list. After partitioning, $\mathcal{A}$ and $\mathcal{B}$ are sorted relative to each other by $\gamma$. Therefore, quick sort simply recurses on $\mathcal{A}$ and $\mathcal{B}$. Quick select does a similar thing. Quick select is an algorithm to find the $n$-th element in the list. If the rank of $\gamma$ equals the target rank, we are done. If the rank of $\gamma$ is less than the target, adjust the target rank and recurse into $\mathcal{B}$. If the rank of $\gamma$ is greater than the target, adjust the target rank and recurse into $\mathcal{A}$. It works like a partial quick sort — it sorts just enough to locate the desired element.
1 | |