Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does quicksort with randomized median-of-three do appreciably better than randomized quicksort?

I was just answering a question about different approaches for picking the partition in a quicksort implementation and came up with a question that I honestly don't know how to answer. It's a bit math-heavy, and this may be the wrong site on which to ask this, so if this needs to move please let me know and I'll gladly migrate it elsewhere.

It's well-known that a quicksort implementation that picks its pivots uniformly at random will end up running in expected O(n lg n) time (there's a nice proof of this on Wikipedia). However, due to the cost of generating random numbers, many quicksort implementations don't pick pivots randomly, but instead rely on a "median-of-three" approach in which three elements are chosen deterministically and of which the median is chosen as the pivot. This is known to degenerate to O(n2) in the worst-case (see this great paper on how to generate those worst-case inputs, for example).

Now, suppose that we combine these two approaches by picking three random elements from the sequence and using their median as the choice of pivot. I know that this also guarantees O(n lg n) average-case runtime using a slightly different proof than the one for the regular randomized quicksort. However, I have no idea what the constant factor in front of the n lg n term is in this particular quicksort implementation. For regular randomized quicksort Wikipedia lists the actual runtime of randomized quicksort as requiring at most 1.39 n lg n comparisons (using lg as the binary logarithm).

My question is this: does anyone know of a way to derive the constant factor for the number of comparisons made using a "median-of-three" randomized quicksort? If we go even more generally, is there an expression for the constant factor on quicksort using a randomized median-of-k approach? I'm curious because I think it would be fascinating to see if there is some "sweet spot" of this approach that makes fewer comparisons than other randomized quicksort implementations. I mean, wouldn't it be cool to be able to say that randomized quicksort with a randomized median-of-six pivot choice makes the fewest comparisons? Or be able to conclusively say that you should just pick a pivot element at random?

like image 804
templatetypedef Avatar asked Feb 15 '11 09:02

templatetypedef


People also ask

Which is better randomized quick sort or quick sort?

The advantage of randomized quicksort is that there's no one input that will always cause it to run in time Θ(n log n) and the runtime is expected to be O(n log n).

Why randomized quick sort is more preferable to normal quicksort?

That's why we consider randomized quicksort better than standard quicksort, because, there is very low probability of bad splits in randomized quicksort. The fact that sorted sequences (which are common in data) is a worst case scenario for quicksort was solved long ago by picking the median element as a pivot.

What is the best case time complexity randomized quick sort?

What is the best case time complexity randomized quick sort? Explanation: Best case time complexity is given in the case when there is equal partitioning of the array about the pivot. It is given by the relation T(n) = 2T(n/2) + n which gives the result O(n log n). 10.

What is the best case performance of quicksort?

The best case for the algorithm now occurs when all elements are equal (or are chosen from a small set of k ≪ n elements).


2 Answers

Here's a heuristic derivation of the constant. I think it can be made rigorous, with a lot more effort.

Let P be a continuous random variable with values in [0, 1]. Intuitively, P is the fraction of values less than the pivot. We're looking to find the constant c such that

c n lg n = E[n + c P n lg (P n) + c (1 - P) n lg ((1 - P) n)].

A little bit of algebra later, we have

c = 1/E[-P lg P - (1 - P) lg (1 - P))].

In other words, c is the reciprocal of the expected entropy of the Bernoulli distribution with mean P. Intuitively, for each element, we need to compare it to pivots in a way that yields about lg n bits of information.

When P is uniform, the pdf of P is 1. The constant is

In[1]:= -1/NIntegrate[x Log[2, x] + (1 - x) Log[2, 1 - x], {x, 0, 1}]  Out[1]= 1.38629 

When the pivot is a median of 3, the pdf of P is 6 x (1 - x). The constant is

In[2]:= -1/NIntegrate[6 x (1 - x) (x Log[2, x] + (1 - x) Log[2, 1 - x]), {x, 0, 1}]  Out[2]= 1.18825 
like image 149
userOVER9000 Avatar answered Sep 22 '22 15:09

userOVER9000


The constant for the usual randomized quicksort is easy to compute because the probability that two elements k locations apart are compared is exactly 2/(k+1): the probability that one of the those two elements is chosen as a pivot before any of the k-1 elements between them. Unfortunately nothing so clever applies to your algorithm.

I'm hesitant to attempt your bolded question because I can answer your "underlying" question: asymptotically speaking, there is no "sweet spot". The total added cost of computing medians of k elements, even O(n1 - ε) elements, is linear, and the constant for the n log n term decreases with the array being split more evenly. The catch is of course constants on the linear term that are spectacularly impractical, highlighting one of the drawbacks of asymptotic analysis.


Based on my comments below, I guess k = O(nα) for 0 < α < 1 is the "sweet spot".

like image 32
a dabbler Avatar answered Sep 24 '22 15:09

a dabbler