Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.

Similar presentations


Presentation on theme: "CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never."— Presentation transcript:

1 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never meant to know. For everything else, there’s Google! ”

2 Searching Querying for something Searching an array for a particular value is a common problem Example: Where is 100 in this array? 1015028910055-2075-1010 A[0] A[1] A[n-1] n=10 …

3 Searching Querying for something Searching an array for a particular value is a common problem Example: Where is 100 in this array? 1015028910055-2075-1010 A[0] A[1] A[n-1] n=10 … Must return location in the array (index=4)

4 The Search Problem 1.Let A be the array to be searched n - the number of elements k - the search target (or key) 2.Question: Does k occur in A? If k appears in A[0], A[1], …, A[n-1]: “found” determine its index i. that is, find i such that A[i] == k Else “not found”: return -1

5 Linear Search // Performs linear search of array for key, from start index to end index. int linearSearch (int[ ] A, int key, int start, int end) { for (int i = start; i < end; i++) { if (key == A[i]) { return i; // “found key”, return index } return -1; // “key not found”, return -1 }

6 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, n elements for value not in array

7 Binary Search Searching a sorted array Fast and efficient

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

9 Binary Search A[n/2]leftright if (A[n/2] == key) return “found” else if (A[n/2] > key) search in left else search in the right

10 Binary Search I int binarySearch( 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” else if (array[middle] > key) return binarySearch(array, key, left, middle-1); else return binarySearch(array, key, middle+1, right); } Recursion

11 Binary Search II int binarySearch(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

12 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!

13 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


Download ppt "CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never."

Similar presentations


Ads by Google