I was going through lecture videos by Robert Sedgwick on algorithms, and he explains that random shuffling ensures we don\'t get to encounter the worst case quadratic time s
What does a random shuffle do to the distribution on the input space? To understand this, let's look at a probability distribution, P
, defined over a set S
, where P
is not in our control. Let us create a probability distribution P'
by applying a random shuffle, over S
to P
. In other words, every time we get a sample from P
, we map it, uniformly at random to an element of S
. What can you say about this resulting distribution P'
?
P'(x) = summation over all elements s in S of P(s)*1/|S| = 1/|S|
Thus, P'
is just the uniform distribution over S
. A random shuffle gives us control over the input probability distribution.
How is this relevant to quicksort? Well, we know the average complexity of quicksort. This is computed wrt the uniform probability distribution and that is a property we want to maintain on our input distribution, irrespective of what it really is. To achieve that, we do a random shuffle of our input array, ensuring that the distribution is not adversarial in any way.