1 / 11

Searching

Searching. CS 105 See Section 14.6 of Horstmann text. The Search Problem. Given an array A storing n numbers, and a target number v, locate the position in A (if it exists) where A[i] = v Example Input: A = {3,8,2,15,99,52}, v = 99 Output: position 4 Notes

haws
Download Presentation

Searching

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. Searching CS 105 See Section 14.6 of Horstmann text

  2. The Search Problem • Given an array A storing n numbers, and a target number v, locate the position in A (if it exists) where A[i] = v • Example • Input: A = {3,8,2,15,99,52}, v = 99 • Output: position 4 • Notes • Array positions (indexes) go from 0..n-1 • When the target does not exist in the array, return an undefined position, such as -1

  3. Algorithm 1: Linear Search Algorithm LinearSearch(A,v): Input: An array A of n numbers, search target v Output: position i where A[i]=v, -1 if not found for i  0 to n - 1 do if A[i] = v then return i return -1

  4. Linear Search time complexity • Worst-case: O( n ) • If v does not exist or if v is the last element in the array • Best-case: O( 1 ) • When the target element v is the first element in the array • Average-case: O( n ) • e.g., if the target element is somewhere in the middle of the array, the for-loop will iterate n/2 times. Still O( n )

  5. Searching in sorted arrays • Suppose the array is arranged in increasing or non-decreasing order • Linear search on a sorted array still yields the same analysis • O( n ) worst-case time complexity • Can exploit sorted structure by performing binary search • Strategy: inspect middle of the search list so that half of the list is discarded at every step

  6. Algorithm 2: Binary Search Algorithm BinarySearch(A,v): Input: A SORTED array A of n numbers, search target v Output: position i where A[i]=v, -1 if not found low  0, high  n-1 while low <= high do mid  (low+high)/2 if A[mid]= v then return mid else if A[mid] < v then low  mid+1 else high  mid-1 return -1

  7. Binary Search time complexity • Time complexity analysis: how many iterations of the while loop?(assume worst-case) • Observe values for low and high • Note that before loop entry,size of search space = n = high-low+1. • On each suceeding iteration, search space is cut in half • Loop terminates when search space collapses to 0 or 1

  8. Binary Search time complexity • search space size iteration numbern 1n/2 2n/4 3… …1 x • x = number of iterations • Observe 2x = n, log 2x = log n,x = log n iterations carried out • Binary Search worst-case time complexity:O( log n )

  9. Binary Search time complexity • Worst-case: O( log n ) • If v does not exist • Best-case: O( 1 ) • When the target element v happens to be in the middle of the array • Average-case: O( log n ) • Technically, some statistical analysis needed here (beyond the scope of this course)

  10. Binary Search (version 2) • Can use recursion instead of iteration • BinSearch(A,v): return BinSearchHelper(A,0,n-1,v) • BinSearchHelper(A,low,high,v): if low > high then return -1 mid  (low+high)/2 if A[mid] = v then return mid else if A[mid] < v then return BinSearchHelper(A,mid+1,high,v) else return BinSearchHelper(A,low,mid-1,v)

  11. Summary • Linear search on an array takes O( n ) time in the worst-case • If the array is sorted, can perform binary search in O( log n ) time • Iterative and recursive versions • Both algorithms run in O( 1 ) time in the best-case • Case where the first element inspected is the target element

More Related