Professional Documents
Culture Documents
Algorithm Analysis
Bernard Chen
Spring 2006
Big Oh Notation
Functions in order of
increasing growth rate
Function
C
LogN
Log2N
N
NlogN
N2
N3
2n
Name
Constant
Logarithmic
Log-squared
Linear
NlogN
Quaratic
Cubic
Exponential
Functions in order of
increasing growth rate
O(N2) algorithm
O(N) Algorithm
template <class Comparable>
Comparable maxSubsequenceSum(const vector<Comparable>& a,
int & seqStart, int &seqEnd){
int n = a.size();
Comparable thisSum = 0, maxSum = 0;
int i=0;
for( int j = 0; j < n; j++){
thisSum += a[j];
if( thisSum > maxSum){
maxSum = thisSum;
seqStart = i;
seqEnd = j;
}else if( thisSum < 0) {
i = j + 1;
thisSum = 0;
}
}
return maxSum;
}//figure 6.8
Figure 6.9
Mathematical
Expression
Relative Rates of
Growth
T(n) = O(F(n))
T(n) = (F(n))
T(n) = (F(n))
T(n) = o(F(n))
T ( n) ( n) : linear time
T ( n) ( n log n) : famous for sorting
T ( n) ( n ) : qualratic time
2
T ( n) ( n ) : polynomial time
k
Cont.
Sequential Search
Unsuccessful search
--n times
Successful search (worst)
--n times
Successful search (average) --n/2 times
Binary Search
Summary
In Class exercises
Q6.14
Q6.15
Answers