Download Searching

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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:
Output:
Notes


A = {3,8,2,15,99,52}, v = 99
position 4
Array positions (indexes) go from 0..n-1
When the target does not exist in the array,
return an undefined position, such as -1
Searching
Slide 2
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
Searching
Slide 3
Linear Search time complexity

Worst-case: O( n )


Best-case: O( 1 )


If v does not exist or if v is the last element
in the array
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 )
Searching
Slide 4
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
Searching
Slide 5
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
Searching
Slide 6
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
Searching
Slide 7
Binary Search time complexity




search space size iteration number
n
1
n/2
2
n/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 )
Searching
Slide 8
Binary Search time complexity

Worst-case: O( log n )


Best-case: O( 1 )


If v does not exist
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)
Searching
Slide 9
Binary Search (version 2)

Can use recursion instead of iteration
BinSearch(A,v):

BinSearchHelper(A,low,high,v):

return BinSearchHelper(A,0,n-1,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)
Searching
Slide 10
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
Searching
Slide 11
Related documents