Theorem: For randomized quicksort, the expected number of comparisons is \(O(n\log n)\).
proof: Denote the input array as \(A=[a_1,a_2,…,a_n]\), while the sorted version is \(S=[s_1,s_2,…,s_n]\). Let \(X\) be the # of comparisons over the execution of quicksort, and \(X_{i,j}\) be the indicator random variable which is defined as,
Then, the total number of comparisons can be expressed as,
Now, let’s focus on the probability of \(X_{i,j}= 1\), which equals to the probability that \(s_i\) and \(s_j\) are compared. As in quicksort, only when one of two elements is selected as pivot, and before that they are in the same sub-array, the two elements need to be compared. That is to say, the probability that \(s_i\) and \(s_j\) are compared equals to the probability that one of \(s_i\) and \(s_j\) is selected as pivot, and before that, they are in the same sub-array, which mean \(s_{i+1},s_{i+2},…,s_{j-1}\) can not be selected as pivot before them. Therefore,
Finally, we obtain that,