c++ - i want to calculate the T(n) for the two algorithms -
i want know time complexity following 2 algorithms
void main (){-----------------------------------------t(n) for(int a=1 ; a<=20 ; a++) {------------? if(a%2==0) -------------? cout<<"value even"; --------? else cout<<"value odd";--------? }
}
void main (){-----------------------------------------t(n) int x=1; {------------? int a=1; -------------? while(a<=n){--------? x=x*a; -------------? a=a+2; -------------? } cout<<x; }
the idea calculate how execution time piece of code - typically algorithm, depends on input. if function takes n input how long take execute if n=5 or n=10. take double long? take same time? or take more double?
in case:
the first program doesn't depend on input o(n)=1.
your second program depends on n. same stuff n/2 times due = + 2. o(n)=n/2. constants typically skipped , 1 write o(n) = n.
if had code this:
for (a=0; < n; a++) { // n times here (b=0; b<n; b++) { // n times here // } }
the execution time change n^2 because both loops iterate n times. each time outer loop executes, inner loop executes n times. since outer loop executes n times, have n*n. o(n) = n^2
Comments
Post a Comment