How do I find this function the growth rate based on the big O notation?
for(i=1; i*i<n; i=i+1)
for(j=1; j<=i; j=j+1)
See the following
when i=1
, innerloop runs 1 time
outloop i=2
, innerloop runs 2 times
outloop i=3
, innerloop runs 3 times
outloop i=4
, innerloop runs 4 times
....
outloop i=sqrt(n)-1
, innerloop runs sqrt(n)-1
times, the loop terminates here
Lest assume that n
is a perfect square
==> f(n) = 1 + 2 + 3 + 4 + ... + sqrt(n) - 1 = (sqrt(n) - 1 )/2 x ( sqrt(n) - 1 + 1)
==> f(n) = ( (sqrt(n)^2)/2 - sqrt(n)/2
==> O( sqrt(n)^2)/2 - sqrt(n)/2 ) = O( n - sqrt(n) )
==> O( n - sqrt(n) )
==> O( n )
So it approximately takes O(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.