CSE 20 Lecture 11 Function, Recursion & Analysis CK Cheng UC San Diego
CSE 3358 Note Set 5
description
Transcript of CSE 3358 Note Set 5
CSE 3358 NOTE SET 5
Data Structures and Algorithms
Complexity of Binary Search
int binarySearch(int arr[], int arrSize, int key) {
int lo = 0, mid, hi = arrSize – 1;
while (lo <= hi) {
mid = (lo + hi) / 2;
if (key < arr[mid])
hi = mid – 1;
else if (arr[mid] < key)
lo = mid + 1;
else return mid;
}
return -1;
}
Big - O
How do I know that T(n) = n2 + 100n + log10 n + 1000is O(n2)?
n F(n) n2 100n log10n 1000Val Val % Val % Val % Val %
1 1,101 1 0.1 100 9.1 0 0.0 1000 90.83
100 21,002 10,000 47.6 10,000 47.6 2 0.001 1000 4.76
10000 101,001,004 100,000,000 99.0 1,000,000 0.99 4 0.0 1000 0.001
Issues with Big - O
Consider:T(n) = O(n2).
When might this be essentially useless information?
Big - Ω
The function f(n) is Ω(g(n)) if there exist positive numbersc and N such that f(n) >= c*g(n) for all n >= N.
Interconnection with Big-O:
f(n) Є Ω(g(n)) iff g(n) Є O(f(n)).
Definition:
Big - Θ
f(n) is Θ(g(n)) if there existpositive numbers c1, c2, and N such that c1*g(n) <= f(n) <= c2*g(n) for all n >= N.
Sometimes called “tight bounds”.
Definition:
Interconnection with Big-O and Big - Ω :
f(n) Є Θ(g(n)) iff f(n) O(g(n)).
?