Average Runtime of Quickselect

后端 未结 3 703
渐次进展
渐次进展 2020-12-05 00:14

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 r

相关标签:
3条回答
  • 2020-12-05 00:39

    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:

    1. max(k,j) = k, therefore i < j <= k
    2. min(k,i) = k, therefore k <= i < j
    3. 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.

    Case 1

    Case 2

    Similar to case 1 so this remains as an exercise. ;)

    Case 3

    We use H_r for the r-th harmonic number which grows approximately like ln(r).

    Conclusion

    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.

    0 讨论(0)
  • 2020-12-05 00:49

    Because

    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%.

    0 讨论(0)
  • 2020-12-05 01:02

    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(n/4) = cn/2 + T(n/8)
    .
    .
    .
    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)

    0 讨论(0)
提交回复
热议问题