Chapter 11 - Analysis of Algorithms
Chapter 11 - Analysis of Algorithms
Chapter 11 - Analysis of Algorithms
ANALYSIS OF ALGORITHMS
PROBLEM TO SOLVE
My program finds all prime numbers between 2 and 1, 000, 000, 000 in
1.37 seconds
Is this
A. Good
B. Bad
C. It Depends
ALGORITHM ANALYSIS
Asymptotic Complexity
Order of Magnitude of the algorithm
Called Big O
Worst Case time to solve problem
Longest possible execution time
More helpful than BEST CASE
Know it will NEVER take more time than this to solve problem
GROWTH FUNCTION
GROWTH FUNCTION: WASHING DISHES
Example 1:
O(n)
T(n) = 30n + 30n
Example 2:
O(n2)
T(n) = 15n2 + 45n
GROWTH FUNCTION: SORTING AN ARRAY
v.
1 2 4 5 7 8 9 10
Lets look at some examples & code first before answering this question!
BIG O EXAMPLES
(2 + 1)2
A. O(n)
B. O(n2)
C. O(n3)
D. O(2n)
BIG O EXAMPLES
(2 + 2)2
A. O(n)
B. O(n2)
C. O(n3)
D. O(2n)
BIG O EXAMPLES
n3 + 100n2 + n
A. O(n)
B. O(n2)
C. O(n3)
D. O(2n)
BIG O EXAMPLES
2n + 100n2 + 45n
A. O(n)
B. O(n2)
C. O(n3)
D. O(2n)
BIG O EXAMPLES
n2n+n22n
A. O(n)
B. O(n2)
C. O(2n)
D. O(n22n)
COMPARISON OF GROWTH FUNCTIONS
BIG O OF LOOPS
count = i;
while (count < n) { Increases by powers of 2,
count *= 2; O(log2n)
// O(1) steps
}
Multiply complexities:
for (int count = 0; count < n; count+=2) {
O(n) * O(n)
for(int count2 = 0; count2 < n; count2++) {
O(n2)
// O(1) steps
}
}
SELECTION SORT