Complexity Analysis of Algorithms: Jordi Cortadella Department of Computer Science
Complexity Analysis of Algorithms: Jordi Cortadella Department of Computer Science
of Algorithms
Jordi Cortadella
Department of Computer Science
Estimating runtime
What is the runtime of g(n)?
void g(int n) {
for (int i = 0; i < n; ++i) f();
}
void g(int n) {
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j) f();
}
void g(int n) {
for (int i = 0; i < n; ++i)
for (int j = 0; j <= i; ++j) f();
}
• Useful for:
– evaluating the variations of execution time with regard
to the input data
– comparing algorithms
T(n)
n0 n
Introduction to Programming © Dept. CS, UPC 6
Big O: example
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1