Algorithm Analysis
Algorithm Analysis
REVIEW
Key Terms:
auto t1 = clock::now()
Approach 2: Modeling/Counting – Physically adding up how many times each expression will execute
int sum = 0;
Independent of computer
for(int i = 0; i < n; i++)
{ Very tedious to compute
sum += i;
}
print(sum);
Total: 3n + 4
n
Time
Input size
Asymptotic Analysis: Some things to know
• All algorithms have a growth rate function T(n) that represents the relationship between their input size
(x) and their execution time (y).
x = 100 elements
• We say that T(n) O (f(n)) (an algorithm with growth rate T(n) is Big-O f(n)) IF…
f(n) is an UPPER BOUND on the function T(n). In other words, T(n) grows SLOWER THAN or at the
SAME RATE as f(n)
• We say that T(n) (f(n)) (an algorithm with growth rate T(n) is Big- f(n)) IF…
f(n) is a LOWER BOUND on the function T(n). In other words, T(n) grows FASTER THAN or at the
SAME RATE as f(n)
(a) 2n6
(b) n5
(c) n5log4(n)
A: (a) and (c) because (a) grows faster and (c) grows at the same rate
Non nested loops? ADD TERMS Drop constant multipliers Drop lower order terms
} } }
} } }