Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I

2 Binary Search 2

3 Linear Search for (int i = 0; i < size; i ++) if (a[i].equals(target)) //if (a[i] == target) return index; return -1; Order of algorithm: O(N) Have to check every element! Why? Not sorted! Quiz is coming? Program due! 3 3 3

4 Binary Search Sorted in Ascending Order
middle middle element is larger than target Target CANNOT be in the 2nd part! Quiz is coming? Program due! middle middle element is smaller than target Target CANNOT be in the 1st part! 4 4 middle 4

5 Binary Search int [] a = new int[MAX_SIZE]; int size, target; // a[] is sorted in ascending order While not done Find index of the middle element If middle element = target return index else if middle element is smaller search the second half else search the first half Return -1 Quiz is coming? Program due! 5 5 5

6 Index of Middle 6 6 Quiz is coming? Program due! 6 lo
middle = (lo+hi)/2 hi middle element is larger than target lo Quiz is coming? Program due! middle = (lo+hi)/2 hi hi = middle -1 middle element is smaller than target lo middle = (lo+hi)/2 hi lo = middle + 1 6 6 6

7 Index of Middle // Integer division middle = (lo + hi) / 2; middle = (0 + 51) / 2; // 25 middle = ( ) / 2; // 38 middle = ( ) / 2; // 31

8 Stop Condition While not done Find index of the middle element If middle element = target return index else if middle element is smaller search the second half else search the first half Return -1 What is the stop condition? lo < hi lo = hi lo > hi What is the while condition? !(lo > hi) lo <= hi Quiz is coming? Program due! 8 8 8

9 Stop Condition lo = p hi = p+1 middle = (p + (p+1)) / 2 = p
middle element is larger than target middle element is smaller than target hi=p-1 lo=p Quiz is coming? Program due! lo = p+1 hi = p+1 hi < lo Done! Not Done! 9 9 9

10 Binary Search lo = p hi = p middle = p
middle element is larger than target middle element is smaller than target hi = p-1 lo = p hi = p lo = p+1 Quiz is coming? Program due! hi < lo hi < lo Done! Done! 10 10 10

11 Binary Search int a[] = new int[MAX_SIZE]; int size; public int binarySearch ( int target ) { int lo = 0, hi = size - 1; while ( lo <= hi ) int middle = ( lo + hi ) / 2; if ( a[middle] == target ) return middle; else if ( a[middle] > target) hi = middle - 1; else lo = middle + 1; } return -1; Quiz is coming? Program due! 11 11 11

12 Big O of Binary Search How many times can N be divided in two before you get down to an array of size 1? n / 2r ≈ 1 n ≈ 2r r = log2(n) Quiz is coming? Program due! 12 12 12

13 Binary Search and Linear Search
Linear Search Binary Search O(n) O(log n) When n = 210 1, When n = 220 1,000,000 20 Quiz is coming? Program due! 13 13 13

14 Search Array of Objects
public interface Comparable <E> { public int compareTo ( E x ); } Return value: negative if (this) less than x 0 if (this) equals x positive if (this) greater than x

15 Class Date public class Date implements Comparable <Date> { . . public int compareTo(Date d) if (year == d.year && month == d.month && day == d.day) return 0; else if ((year < d.year) || (year == d.year && month < d.month) || (year == d.year && month == d.month && day < d.day)) return -1; else return 1; }

16 Class DateList public class DateList { private int count; private Date[] list; public DateList(int listSize) list = new Date[listSize]; count = 0; } . . . Quiz is coming? Program due!

17 Class DateList public class DateList { // Insertion in sorted order // Assuming list size is large enough public void insert(Date d) int index = count; while (index > 0 && list[index - 1].compareTo(d) > 0) list[index] = list[index - 1]; index --; } list[index] = d; count ++; Quiz is coming? Program due!

18 Class DateList public class DateList { // What is the Big O? // O(N) public void insert(Date d) int index = count; while (index > 0 && list[index - 1].compareTo(d) > 0) list[index] = list[-- index]; } list[index] = d; count ++; Quiz is coming? Program due!

19 Class DateList public class DateList { // What is the Big O? // O(N) public boolean remove(Date d) int index = BinarySearch(d); if (index >= 0) count --; for (int i = index; i < count; i ++) list[i] = list[i + 1]; return true; } return false; Quiz is coming? Program due!

20 Class DateList public class DateList { private int count; private Date[] list; public int BinarySearch(Date target) int lo = 0, hi = count - 1; while ( lo <= hi ) int mid = ( lo + hi ) / 2; int result = list[mid].compareTo(target); if ( result == 0 ) return mid; if ( result > 0 ) hi = mid - 1; else lo = mid + 1; } return -1; Quiz is coming? Program due!

21 public class SortedList { private Comparable [] items; private int count; public int BinarySearch(Comparable x) int lo = 0, hi = count - 1; while ( lo <= hi ) int mid = ( lo + hi ) / 2; int result = items[mid].CompareTo(x); if ( result == 0 ) return mid; if ( result > 0 ) hi = mid - 1; else lo = mid + 1; } return -1; Quiz is coming? Program due!

22 Tracing Binary Search Array items[] has 9 values and sorted in ascending order. Binary search is used to find a target. Tracing the execution assuming the target is larger than all values in the array. low mid high Number of comparisons: 4 Quiz is coming? Program due! 22 22 22

23 Array items[] has 100 values and sorted in ascending order
Array items[] has 100 values and sorted in ascending order. Binary search is used to find a target. Tracing the execution assuming the target is not in the array and its value is between items[40] and items[41]. low mid high Number of comparisons: 7 Quiz is coming? Program due! 23 23 23

24 Due 11 pm, Monday Grace 11 pm, Wednesday (-5)
Prog 5 Due 11 pm, Monday Grace 11 pm, Wednesday (-5)

25 Prog 6 Game Group sign up Plan in SE Tool Friday, Nov 30, by 11:50 am
Could be the same as for Prog5 Plan in SE Tool Monday, Dec 3, by 11 p.m.

26 Lab 12 Due 11 pm, Monday, Dec 3 Must do it before Prog 6
Group assignment Same groups as for Prog6

27 Counting Exercise for I = (N - 1) down to 3 for J = (I + 1) to (N + 2) if ---- The total number of times the “if” is executed is: ____________ Pass I First of J Last of J Times of “if” executed N-1 N-2 N-3 . 5 4 3 N N-1 N-2 . 6 5 4 N+2 . 3 4 5 . N-3 N-2 N-1

28 Counting Exercise (N-3) + (N-2) + (N-1) = (Last + First) * (Number of terms) / 2 = (N+2) * ((N-1) – 3 + 1) / 2 = (N+2) * (N-3) / 2 = (N2 – N – 6) / 2

29 Quiz 5 Wednesday, Nov 28

30 Binary Search Exercise
Turn in at the beginning of class Friday, Nov 30


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google