Average Runtime of Quickselect
Wikipedia states that the average runtime of quickselect algorithm (Link) is O(n). However, I could not clearly understand how this is so. Could anyone explain to me (via recurrence relation + master method usage) as to how the average runtime is O(n)?
algorithm big-o
Dante May Code
RaviRavi
we already know which partition our desired element lies in.
We do not need to sort (by doing partition on) all the elements, but only do operation on the partition we need.
As in quick sort, we have to do partition in halves *, and then in halves of a half, but this time, we only need to do the next round partition in one single partition (half) of the two where the element is expected to lie in.
It is like (not very accurate)
n + 1/2 n + 1/4 n + 1/8 n + ..... < 2 n
So it is O(n).
Half is used for convenience, the actual partition is not exact 50%.
Dante May CodeDante May Code
in the worst-case it's O(n^ 2) – simpatico May 23 '12 at 16:03
dante - average case is O(n), worst case is O(n^2), because as you said the partition is not guaranteed to be 50%. eg. in a 10 element array in the case where you select the worst possible partition each time you'll end up with n + 9/10n + 8/10n + ... + 1/10n i.e. n^2. See en.wikipedia.org/wiki/Selection_algorithm for a more detailed explanation. – shreddd Dec 28 '12 at 22:31
It would be exactly n/2 if the partition was guaranteed to be 50%. Using the sum of a geometric progression formula a1/(1 - q) – Tito Nov 2 '13 at 15:59
@shreddd But isn't big Oh only used for measuring worst case? – committedandroider Mar 15 '15 at 0:37
@committedandroider No, it's used to classify functions which grow maximally with a certain asymptotic speed. So in this case it means that the expected value (on input uniformly at random) of the runtime doesn't grow faster than the function c*n for some constant c. – maybeshewill Feb 24 '16 at 18:41
To do an average case analysis of quickselect one has to consider how likely it is that two elements are compared during the algorithm for every pair of elements and assuming a random pivoting. From this we can derive the average number of comparisons. Unfortunately the analysis I will show requires some longer calculations but it is a clean average case analysis as opposed to the current answers.
Let's assume the field we want to select the k-th smallest element from is a random permutation of [1,...,n]. The pivot elements we choose during the course of the algorithm can also be seen as a given random permutation. During the algorithm we then always pick the next feasible pivot from this permutation therefore they are chosen uniform at random as every element has the same probability of occurring as the next feasible element in the random permutation.
There is one simple, yet very important, observation: We only compare two elements i and j (with i<j) if and only if one of them is chosen as first pivot element from the range [min(k,i), max(k,j)]. If another element from this range is chosen first then they will never be compared because we continue searching in a sub-field where at least one of the elements i,j is not contained in.
Because of the above observation and the fact that the pivots are chosen uniform at random the probability of a comparison between i and j is:
2/(max(k,j) - min(k,i) + 1)
(Two events out of max(k,j) - min(k,i) + 1 possibilities.)
We split the analysis in three parts:
max(k,j) = k, therefore i < j <= k
min(k,i) = k, therefore k <= i < j
min(k,i) = i and max(k,j) = j, therefore i < k < j
In the third case the less-equal signs are omitted because we already consider those cases in the first two cases.
Now let's get our hands a little dirty on calculations. We just sum up all the probabilities as this gives the expected number of comparisons.
Similar to case 1 so this remains as an exercise. ;)
We use H_r for the r-th harmonic number which grows approximately like ln(r).
All three cases need a linear number of expected comparisons. This shows that quickselect indeed has an expected runtime in O(n). Note that - as already mentioned - the worst case is in O(n^2).
Note: The idea of this proof is not mine. I think that's roughly the standard average case analysis of quickselect.
If there are any errors please let me know.
maybeshewillmaybeshewill
3,33411 gold badge1313 silver badges88 bronze badges
Thanks, that was the rigorous answer I was looking for. – I want to make games Feb 2 '17 at 18:32
There is a mistake in your analysis of Case 1. $\sum _{d=1}^{k-1} \neq \sum _{d=1} ^{k-1} \frac{d}{d+1} $ – john doe Jan 21 '18 at 22:06
@johndoe I'm afraid I don't understand. Do you refer to the equality after the line break in the analysis of case 1? This comes from $\frac{d}{d+1} = \frac{d+1}{d+1} - \frac{1}{d+1} = 1 - \frac{1}{d+1}$. – maybeshewill Feb 7 '18 at 23:56
@maybeshewill I meant the equality just before that – john doe Feb 8 '18 at 14:49
@johndoe This is just "inverting" the sum. For the left side the first term of the sum is (k-1)/(d+1) and the last one 1/(d+1); for the right side the first term is 1/(d+1) and the last one is (k-1)/(d+1). Thus, the sums are equal. – maybeshewill Feb 12 '18 at 23:57
In quickselect, as specified, we apply recursion on only one half of the partition.
Average Case Analysis:
First Step: T(n) = cn + T(n/2)
where, cn = time to perform partition, where c is any constant(doesn't matter).
T(n/2) = applying recursion on one half of the partition.
Since it's an average case we assume that the partition was the median.
As we keep on doing recursion, we get the following set of equation:
T(n/2) = cn/2 + T(n/4)
T(2) = c.2 + T(1)
T(1) = c.1 + ...
Summing the equations and cross-cancelling like values produces a linear result.
c(n + n/2 + n/4 + ... + 2 + 1) = c(2n) //sum of a GP
Hence, it's O(n)
AdityaAditya
This is no average case analysis. For an average case analysis you have to consider every possible pivoting and not only the expected value of the pivot element. – maybeshewill Sep 11 '14 at 17:48
Not the answer you're looking for? Browse other questions tagged algorithm big-o or ask your own question.
Why is the runtime of the Selection Algorithm O(n)?
recur by using the pivot in Select algorithm
QuickSelect average time complexity O(n) [HOW?]
Understanding this derivation of Average Runtime of Quickselect
How does run time for quick select, comes out to be, CN = 2 N + 2 k ln (N / k) + 2 (N – k) ln (N / (N – k))?
Algorithm for 2-Satisfiability problem
Meaning of average complexity when using Big-O notation
Quickselect Algorithm - Simplified Explanation
Difference between Quickselect & Binary Search Selection
How to find the run time of this algorithm to find if a binary tree is balanced?
Calculating average case complexity of Quicksort
How can I make my quickselect algorithm faster
Why recurrence relations arise in analyzing the running times of recursive algorithms?