130 likes | 285 Views
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Searching. “ Some things Man was never meant to know. For everything else, there’s Google! ”. Course Lecture Slides 28 May 2010. Ganesh Viswanathan. Searching. Querying for something
E N D
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Searching “ Some things Man was never meant to know. For everything else, there’s Google! ” Course Lecture Slides28 May 2010 GaneshViswanathan
Searching • Querying for something • Searching an array for a particular value is a common problem • Example: Where is 100 in this array? n=10 A[1] … A[0] A[n-1]
Searching • Querying for something • Searching an array for a particular value is a common problem • Example: Where is 100 in this array? n=10 A[1] … A[0] A[n-1] Must return location in the array (index=4)
The Search Problem • Let A be the array to be searched • n - the number of elements • k- the search target (or key) • Question: Does k occur in A? • If k appears in A[0], A[1], …, A[n-1]: “found” determine its indexi. • that is, find i such that A[i] == k • Else “not found”: return -1
Linear Search • // Performs linear search of array for key, from start index to end index. • intlinearSearch (int[ ] A, int key, int start, int end) { • for (inti = start; i < end; i++) { • if (key == A[i]) { • return i; // “found key”, return index • } • } • return -1; // “key not found”, return -1 • }
Linear Search • Advantages: • easy and straightforward algorithm. • array can be in any order. • Disadvantages: • slow and inefficient! • Given an array of n elements, it examines • n/2 elements on average for value in array, • nelements for value not in array
Binary Search • Searching a sorted array • Fast and efficient
Binary Search • 1. get the middle element; • 2. if middle element equals searched value, then the algorithm stops (return index); • 3. otherwise, two cases are possible: • * searched value is less than the middle element. In this case, go to the step 1 for the part of the array before middle element. • * searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array after middle element.
Binary Search left A[n/2] right if (A[n/2] == key) return “found” else if (A[n/2] > key) search in left else search in the right
Binary Search I intbinarySearch(int[] array, int key, int left, int right) { if (left > right) return -1; // “not found” int middle = (left + right) / 2; if (array[middle] == key) return middle; // “found” elseif (array[middle] > key) returnbinarySearch(array, key, left, middle-1); else returnbinarySearch(array, key, middle+1, right); } Recursion
Binary Search II intbinarySearch(int[] array, int key, int left, int right){ while (left < right) { int middle = (left + right)/2; // Compute mid point if (array[mid] > key) { right = mid; // repeat search in bottom half } else if (array[mid] < key) { left = mid + 1; // Repeat search in top half } else { return mid; // “found” } } return -1; // “not found” } Iteration
Binary Search • Advantages: • fast and efficient! • Disadvantages: • array elements have to be in sorted order. • For an array of 1 million elements, linear search takes 500,000 comparisons to find the key, binary search takes only 20 comparisons!
Search Linear search in arbitrary array: Reduce search region by 1 in each step Binary search in sorted array: Reduce search region by half in each step