2,458 views
2 votes
2 votes
i read  time taken loop will run is o(n/2)  but if n=8  loop becomes for(i=4;i<=8;++i)   it print 4,5,6,7,8 , so it runs 5 times then how n/2 can be running time please help :

2 Answers

3 votes
3 votes
Whenever we are asked to find time complexity we need to find approximate answer not exact answer and we will get approximation only for large values.
0 votes
0 votes
let us understand in tis way. our ques is for(i=n/2; i<=n; i++)

let n=8 , then i=8/2 =4

now increment i by1, we get,

i=4 which means 4+0

i=5 => 4+1

i=6 => 4+2

i=7 => 4+3

i=8 => 4+4

so for n value; n=4+k

and k=n-4 = O(n-4)= O(n)

as per your ques. put n=8 in above eq. then k=8-4= 4 i.e half of n ( n/2).

likewise take any value of n , the answer will be same i.e n/2.

Related questions

0 votes
0 votes
3 answers
1
anonymous asked Jun 14, 2017
1,208 views
0 votes
0 votes
0 answers
3
1 votes
1 votes
1 answer
4
sripo asked Nov 14, 2018
1,605 views
T(n)=T(n/2)+2; T(1)=1when n is power of 2 the correct expression for T(n) is:a) 2(logn+1)b) 2lognc)logn+1d)2logn+1