1 / 11

Randomized Quick Sort Algorithm

Randomized Quick Sort Algorithm. Lecture Note – 1 Prepared By: Muhammed Miah. Randomized Quick Sort. In traditional Quick Sort, we always pick the first element as the pivot for partitioning.

afia
Download Presentation

Randomized Quick Sort Algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Randomized Quick Sort Algorithm Lecture Note – 1 Prepared By: Muhammed Miah 01/16/2008

  2. Randomized Quick Sort • In traditional Quick Sort, we always pick the first element as the pivot for partitioning. • The worst case runtime is O(n2) while the expected runtime is O(nlogn) over the set of all input. • Therefore, some input are born to have long runtime, e.g., an inversely sorted list. 01/16/2008

  3. Randomized Quick Sort • In randomized Quick Sort, we pick randomly an element as the pivot for partitioning. • The expected runtime of any input is O(nlogn). • Randomized Quick Sort is a probabilistic algorithm 01/16/2008

  4. Randomized Quick Sort (S) • Input: a set of numbers S, S = {S1, S2, S3, …, Sn} • If |S| > 1 Then, • Select an element X from S at random • Partition S into 3 parts: • Random Quick Sort (L); • Random Quick Sort (R); L X R 01/16/2008

  5. Randomized Quick Sort • Example: • S = {2, 1, 3, 5, 4} 1st pivot 2 1 2 3, 5, 4 2nd pivot 3rd pivot 1 5 sorted 3, 4 5 4 4th pivot 3 4 5th pivot 3 01/16/2008

  6. Randomized Quick Sort • Properties: • Running time is unpredictable • It has randomization built into it • Nothing to do with input order • Worst unequal division – when one side has zero number and other side has all numbers • Worst case running time n2 01/16/2008

  7. Analysis of “Expected” Running Time of Randomized QS • Assume, S = {1, 2, 3, 4, …, n) Let us consider a Boolean variable, Xij 1 ≤ i, j ≤ n, j > i Xij = 1 if it is compared with j Xij = 0 otherwise # of variables we can create ≈ n2 millions of trees are generated AVG. 01/16/2008

  8. Analysis of “Expected” Running Time of Randomized QS • Xij is a random variable E[∑∑ Xij] = ∑∑ E[Xij] linearity of expectation, E[X + Y] = E[X] + E[Y] E[Xij] = how often i and j compared n-1 n n-1 n i=1 j>i i=1 j>i Horizontal definition Vertical definition 01/16/2008

  9. Analysis of “Expected” Running Time of Randomized QS • Level wise left to right (LR) order: consider set of numbers {1, 2, …, 10} assume we got the tree: level wise order: 6 4 8 3 5 9 1 10 2 If Min(LR[i], LR[j] > Min(LR of i+1, i+2, …, j-1), then i is NOT compared to j otherwise it is compared 6 4 8 5 7 9 3 1 10 2 01/16/2008

  10. Analysis of “Expected” Running Time of Randomized QS P(Xij = 1) = 2/(j-i+1) E[Xij] = 2/(j-i+1) ∑∑ E[Xij] = ∑∑ 2/(j-i+1) ≈ ∑∑ 2/(j-i) ≈ ∑∑ 2/k let, j-i = k ≤ ∑∑ 2/k n-1 n n n i=1 j>i i=1 j>i n n n n-i i=1 J=i i=1 k=1 n n i=1 k=1 01/16/2008

  11. Analysis of “Expected” Running Time of Randomized QS 2∑∑ 1/k = 2n(∑ 1/k) ∑ 1/k = 1/1 + 1/2 + 1/3 + … + 1/n (Harmonic Series) 2n(∑ 1/k) = 2n logn ≈ O(nlogn) So, “Expected” running time of randomized quick sort = O(nlogn) n n n k=1 i=1 k=1 n k=1 n k=1 01/16/2008

More Related