Sunday 15 August 2010

algorithm - Calculating time efficiency of a chunk of code -



algorithm - Calculating time efficiency of a chunk of code -

i have chunk of code:

int f = 0; for(int i=1 ; i<m ; i=i*2) { for(int l=500 ; l<700 ; l++) { f++; } for(int j=n ; j>0 ; j=j/2) { f++; } for(int k=0 ; k<m ; k=k+3) { f++; } f += 10; }

where m , n given parameters.

so far have calculated this:

the main cycle increments exponentially, means time efficiency o(log m)

the first cycle consumes constant time o(200), not impact time efficiency of code.

the sec cycle decrements 1/2 of value has n every itteration, hence time efficiency o(log n)

the 3rd cycle increments 3 every itteration => has o(m/3) time efficiency, don't need constant, o(m)

all in all, when combine o(log(m) * (log n + m)).

is analysis correct?

using sigma notation, may methodically find:

algorithm big-o

No comments:

Post a Comment