1 / 13

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

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

harmon
Download Presentation

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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]

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

  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

  5. 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 • }

  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, • nelements 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 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

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

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

  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

More Related