Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Algorithms 3111725

Download as odp, pdf, or txt
Download as odp, pdf, or txt
You are on page 1of 1

During the running of the procedure Randomized-

Quicksort, how many calls are made to the


random-number generator Random in the worst
case? How about in the best case? Give your
answer in terms of Θ-notation.
演算法如下
Randomized-Partition(A,p,r)
1 i ← Random(p, r)
2 exchange A[r] ↔ A[i]
3 return Partition(A,p,r)
Randomized_Quicksort(A,p,r)
1 if p < r
2 then q ← Randomized-Partition(A, p, r)
3 Randomized_Quicksort(A,p,q)
4 Randomized_Quicksort(A,q+1,r)
每次 Randomized-Partition 都會呼叫一次 Random,因此這個問題
等於在問 Randomized-Partition會被呼叫幾次,顯然 worst case
是會呼叫 n-1 次,best case 會呼叫 ⌊n/2⌋ 次,只是題目要用  來
表示,因此都是 (n)。

You might also like