0

I have recently understand the Big Oh notation. I have recently came across the example given in the book.

void Function(int n)
{
int i=1 ;
int s=1 ;
while( s <= n)
{
i++ ;
s= s+i ;
print(\*");
}
}

I don't know how does the author arrives at the time complexity of the above algorithm as O(√n). I just want to understand the process for arriving at this conclusion?

1 Answer 1

2

It can easily be seen as s is growing quadratic in terms of number of iteration.

s = 1 // set as 1 initially Now we are adding S = s + i // Where i increasing linearly by one unit in each iteration

//So it's just a addition i.e. s = 1 + 2 + 3 +4 ....+i, which will sum up to s = i(i+1)/2 Hence s = i(i+1)/2 = (i^2+i)/2 where i is the number of iteration.

Now, In i iteration we are getting s = (i^2+i)/2 To get s >=n we required only √n iterations. Hence the time-complexity of given program will be O(√n).

Sign up to request clarification or add additional context in comments.

7 Comments

I don't understand how you identify that s=i(i+1)/2 ? is there any mathematics topics that needs to be read for this ? How does it become O(√n).Apology for asking too many questions but I am putting my all efforts to understand the time complexity analysis.
I have edited the answer, see if you can understand.
Thanks Shravan. I got that how do you ge that expression s=(i^2+i)/2.But I dont' understand how does it become O(√n). Apology once again for eating your head.
@Beast so s=(i^2+i)/2 and you want s=>n which is the same as (i^2+i)/2 >=n . Please note that i denotes the number of times that loop will be executed since you increase it by 1 each time. The (i^2+i)/2 >=n inequality is quadratic and (for simplicity) you can reduce it to i^2>=n => i>=√n.
I would say no, if you understand the basic concept of mathematics like A.p, G.P, recursion, Logrithm and polynomial functions. You can see this book cs.princeton.edu/courses/archive/fall07/cos433/mathcs.pdf if you really want something.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.