Time Complexity of Randomized Quick Sort

Consider the randomized quick sort (i.e. the pivot is randomly chosen).

Let the sorted array \(A = [b_1, \dots, b_n]\). Put \(A_{ij} = \{b_i \text{ is compared to } b_j\}\).

Since \(b_i\) is compared to \(b_j\) iff \(b_i\) or \(b_j\) is first pivot chosen from \([b_i, \dots, b_j]\), thus each pair is compared at most once, and we have

\[\mathbb P(A_{ij}) = \frac{2}{j-i+1}, \]

and the total number of comparisions performed

\[X = \sum_{i=1}^{n-1}\sum_{j=i+1}^{n}1_{A_{ij}}. \]

Therefore,

\[\mathbb EX < \sum_{i=1}^{n-1}\sum_{k=1}^n \frac{2}{k}=O(n\log n). \]

posted @ 2019-07-04 09:45  resolvent  阅读(139)  评论(0编辑  收藏  举报