Professional Documents
Culture Documents
Data Structure and Algorithms - One
Data Structure and Algorithms - One
Data Structure and Algorithms - One
of Algorithms
Asymptotic Performance
Correctness
Reference
About
Visit:
http://172.16.1.3/~halder/
Contact Me:
halder@iitp.ac.in
Meeting Time:
Wednesday Afternoon (not strict to follow always)
Asymptotic Performance
InsertionSort(A, n) {
for j = 2 to n {
key = A[j]
i = j - 1;
while (i > 0) and (A[i] > key) {
A[i+1] = A[i]
i = i - 1
}
A[i+1] = key
}
}
Analysis: Running Time
Analysis: Running Time
Analysis: Running Time
• Best Case
• Average Case??
Analysis: Running Time
• Simplifications
Ignore actual and abstract statement costs
Order of growth is the interesting measure:
o Highest-order term is what counts
Remember, we are doing asymptotic analysis
As the input size grows larger it is the high order term that
dominates
Asymptotic Notation
(Upper Bound)
• Proof
Suppose runtime is an2 + bn + c
o If any of a, b, and c are less than 0 replace the constant
with its absolute value
an2 + bn + c (a + b + c)n2 + (a + b + c)n + (a + b + c)
3(a + b + c)n2 for n 1
Let c’ = 3(a + b + c) and let n0 = 1
• Question
Is InsertionSort O(n3)?
Is InsertionSort O(n)?
Big O Fact
n
o- and - Asymptotic Notations
• Intuitively,
() is like =
Practical Complexity
250
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Practical Complexity
500
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Practical Complexity
1000
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 3 5 7 9 11 13 15 17 19
Practical Complexity
5000
4000
f(n) = n
f(n) = log(n)
3000
f(n) = n log(n)
f(n) = n^2
2000 f(n) = n^3
f(n) = 2^n
1000
0
1 3 5 7 9 11 13 15 17 19
Proof By Induction