Evaluate Big O loop worst case and condition statements

29 Views Asked by At

Understanding big O, worst case in a loop. Im supposed to count assignation and comparisons, I dont get why?

The other doubt I have is if the worst case is seeing/evaluated to infinity or for all the executions that are in agreement with the conditon?

Here the comparison is in function of the for loop, which is done n times so comparisons are done n times as well. Since assignations are in function of comparisons, they are done n/2 times. Multiplying them all together gives me (n+n/2) x (n+n/2) = (n^2)+3/2(n^2) = n^2.

If I understood things correctly, I will multiply the two for loops or the inner requirements(assignments and comparisons), to get big O?

test void (char tab [50]){
   a = 0;
   for (i = 0; i<= tab.length()-1; i++){  <--n
       if(i % 2 == 1)                     <----------- n
           a = a - tab[i];                <----------- n/2 
   }
   for (j = 0; j <= tab.length()-1; i++){ <--n
       if(j % 2 == 0)                     <----------- n
           a = a - tab[j];                <----------- n/2
   }
}