in Algorithms retagged by
1,024 views
3 votes
3 votes
int fun(int n)
    {
    int s=0,i;
    if(n<=1) return 1;
    for(i=1; i*i<n; i++)
    s+=n;
    return fun(n/4)+fun(n/4)+s;
    }


 

what will be the time complexity, returning value and no. of recursive calls of the above-given code?

in Algorithms retagged by
1.0k views

1 comment

Is the complexity √n logn ?
1
1

3 Answers

2 votes
2 votes
Best answer

1.Recurrence relation  for time complexity

will be T(n)=2T(n/4)+Root(n)

Because complexity of the loop is root(n)

By using case 2 of master theorem we have T(n)=theta(root(n)logn).

2.Recurrence relation  for return value

T(n)=2T(n/4)+Root(n)*n

because the function return value 2 time fun(n/4)+root(n)*n

by solving using  case 3 of master theorem we have solution of recurrence is theta(root(n)*n)

3.Recurrence relation for no of calls

T(n)=2T(n/4)+1

by solving using  case 1 of master theorem we have solution of recurrence is theta(root(n))

Correct me if i am wrong somewhere.

selected by

4 Comments

yup, Right!!
0
0
How you got recurrence relation for return value? I didn't get Root(n)*n . Please help me...
0
0
for(i=1; i*i<n; i++)
    s+=n;

S is incremented by value of n each time in loop and loop runs  till i^2<n or i<root(n)

So loop runs for root(n) times and each time value of s incremented by n.

Total=root(n)*n

0
0
thanks...! got it now:)
0
0
0 votes
0 votes
  • Asymptotic Time complexity = $n^{0.5} \cdot \log_2 n$
  • Asymptotic value = $k.n^{1.5}$ where $k \approx 1.32$
by

1 comment

what will be the recurrence relation???
0
0
0 votes
0 votes
recurrence relation will be

2T(n/4)+root(n)      and T(n)=theta(root(n)iogn)

1 comment

Recurrence relation for what ?
0
0

Related questions