Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Analysis of algorithms
Measuring eciency of an algorithm
Example 1: Sorting
Sorting an array with n elements
Example 1: Sorting
Telephone directory for mobile phone users in India
27780 hours
1157 days
3 years!
About 3 seconds
Input size
Running time depends on input size
Input size n
Input size
How do we fix input size?
Orders of magnitude
When comparing t(n) across problems, focus on
orders of magnitude
Ignore constants
tmp x
x y
y tmp
Typical functions
Feasibility limit
Even n2 is infeasible for
inputs of size 1 million
(10 lakhs)
i 0
Dicult to compute
Ignore constants
t(n) cg(n)
for every n n0
Examples: Big O
100n + 5 is O(n2)
100n + 5
100n + n, for n 5
2
= 101n 101n , so n0 = 5, c = 101
Alternatively
100n + 5
Examples: Big O
100n2 + 20n + 5 is O(n2)
100n2 + 20n + 5
125n2
n0 = 1, c = 125
Examples: Big O
n3 is not O(n2)
Useful properties
If
f1(n) is O(g1(n))
f2(n) is O(g2(n))
max(O(gA(n)),O(gB(n)))
Binary search
Searching for K in unsorted list A takes time O(n)
What if A is sorted?
Binary Search
How long does this take?
Binary search
bsearch(K,A,left,right)
// integer division
Summary
Measure worst case time complexity