<P> Bucket sort with two buckets is very similar to quicksort; the pivot in this case is effectively the value in the middle of the value range, which does well on average for uniformly distributed inputs . </P> <P> A selection algorithm chooses the kth smallest of a list of numbers; this is an easier problem in general than sorting . One simple but effective selection algorithm works nearly in the same manner as quicksort, and is accordingly known as quickselect . The difference is that instead of making recursive calls on both sublists, it only makes a single tail - recursive call on the sublist that contains the desired element . This change lowers the average complexity to linear or O (n) time, which is optimal for selection, but the sorting algorithm is still O (n). </P> <P> A variant of quickselect, the median of medians algorithm, chooses pivots more carefully, ensuring that the pivots are near the middle of the data (between the 30th and 70th percentiles), and thus has guaranteed linear time--O (n). This same pivot strategy can be used to construct a variant of quicksort (median of medians quicksort) with O (n log n) time . However, the overhead of choosing the pivot is significant, so this is generally not used in practice . </P> <P> More abstractly, given an O (n) selection algorithm, one can use it to find the ideal pivot (the median) at every step of quicksort and thus produce a sorting algorithm with O (n log n) running time . Practical implementations this variant are considerably slower on average, but they are of theoretical interest because they show an optimal selection algorithm can yield an optimal sorting algorithm . </P>

When does the worst case of quick sort occur