So choosing a pivot at random has O(n2) running at worst case but when the pivot is chosen as the average of min value and max value of the list you get a worst case O(n log n).
Of course there are the added 2*O(n) on each recursion due to finding the min and max values as opposed to the constant O(1) of the random generator has. When implementing this as the pivot you get the list sorted at the leaves of the recursion tree instead in the standard algorithm elements get sorted from the root to leaves.
When implementing instead of the pivot being a value on the list it is just a number compared to so this is not the standard quicksort but my question still applies.
Below is my poorly written pseudo code:
func sort(List n):
if n.length < 2
return n;
min = n.minValue
max = n.maxValue
avg = (min+max) /2
List left = list of elements in n less than avg
List right = list of elements in n greater than avg
sort(left)
sort(right)
Your algorithm suffers O(n2) if you choose average of min value and max value as pivot when the list contains the following elements:
1, 3, 7, 15, 31, 63, ..., 2n-1
You could find that for each pass of your algorithm, the right
part always has only 1 element.
Three things:
You can get the max and min in one pass of the list, so actually we add 1*O(n) for each pass. However...
The average of max and min doesn't guarantee O(nlog(n)), since the average of the maximum and minimum is not necessarily the median value. If you have an already sorted list (1,10,100,1000,10000), this would actually give an O(n^2) solution, which is really bad for an already sorted list (and fairly likely to happen).
Choosing a random pivot is statistically likely to give you something close to the median. Take a random number from a list, 50% of the time this number is in the middle 50% of the list, meaning in the worst case it has 75% of the list to one side and 25% on the other. Obviously performance is even better when we chose closer to the true median.
来源:https://stackoverflow.com/questions/29980907/why-choose-a-random-pivot-in-quicksort