sample(A[], p, r)
{
if(p=r) return 1;
sum = 0;
for(int i = 1; i < n(?); i++)
sum = sum + A[i];
int q = (p+r)/2;
int t = sum + sample(A,p,q) + sample(A, q+1, r);
return t;
}
I use iteration method to solve this question in image. that code is right? i can't make recurrence relation. maybe T(n) = T...? where is "n"
You are breaking the size of recursion by factor 2, and also have a loop with n
iteration. Hence, asymptotically, T(n) = 2T(n/2) + n = Theta(n log(n))
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.