CSE 3358 Note Set 5

8
CSE 3358 NOTE SET 5 Data Structures and Algorithms

description

CSE 3358 Note Set 5. Data Structures and Algorithms. Google Chrome. http://www.google.com/googlebooks/chrome/#. Complexity of Binary Search. int binarySearch ( int arr [], int arrSize , int key) { int lo = 0, mid, hi = arrSize – 1; while (lo

Transcript of CSE 3358 Note Set 5

Page 1: CSE 3358  Note Set  5

CSE 3358 NOTE SET 5

Data Structures and Algorithms

Page 2: CSE 3358  Note Set  5

Google Chrome

http://www.google.com/googlebooks/chrome/#

Page 3: CSE 3358  Note Set  5

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;

}

Page 4: CSE 3358  Note Set  5

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

Page 5: CSE 3358  Note Set  5

Issues with Big - O

Consider:T(n) = O(n2).

When might this be essentially useless information?

Page 6: CSE 3358  Note Set  5

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:

Page 7: CSE 3358  Note Set  5

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)).

Page 8: CSE 3358  Note Set  5

?