Reputation: 15
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"
Upvotes: 0
Views: 76
Reputation: 18838
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))
.
Upvotes: 1