Professional Documents
Culture Documents
Ex. linearequation
for (i = 0,sum=0; i < n; i++)
sum += a[i];
Solution:
Step 1: f(n)= 2+ ---> Count how many initializations are there in the code
Step 2: f(n)= 2+ n ---> How many times do we proccess the loop (its the i<n) always put n
Step 3: f(n)= 2+ 2n ---> (altering) what changes if n changes (sum and i) then put it before n
Step 4: O(n) ---> Find Big 0 (2n but remove 2)
1. Constant
2. Logarithmic equation = single loop with the process inside * or /
(radics is dependent on what multiplies/divides it)
3. Linear equation = single loop with the process inside + or -
4. Linearithmic = Nested loop where the inner most loop is a * or / (outerloop)x(inneloop)
First loop does not includes secondloop for the declaration
Logarithmic is not included in the alterings
6. Cubic = 3 loops
First loop does not includes secondloop for the declaration
First loop altering includethe second loop
7. Exponential
8. Factorial