Binary Search

advertisement
Sequential Search
seqsearch( A: array; x: data_type, n: int) : integer
// Return position of x, or 0 if x not in A
i1
while( i  n and x  A[ i ])
ii+1
if (i  n) then return(i)
else return(0)
Best case:
Worst case:
Determining TA(n):
Case 1: x is on the list
Average Case Sequential Search (continued)
Case 2: x may not be on the list.
Problem variants
You are doing a sequential search on a list with the following
characteristics: The item you’re looking for is on the list and
the probability it is in the first half of the list is twice the
probability it is in the second half.
Binary Search
binsearch(var A: array; x: data_type, n: int)
// array A is sorted in increasing order
i  1 // left endpoint of search interval
j  n // right endpoint of search interval
while (i  j)
(1) m  (i + j)/2
(2) if (A[m] < x) then i  m + 1
(3) elseif (x < A[m]) then j  m - 1
else return(m)
return(NOT_FOUND)
Analysis:
Basis case:
n=2
sequential
binary
n = 210
n = 220
complexity
Maximum subsequence sum problem
(References: CACM 27 (Sept. 1984), 865 - 870 and
http://www.student.cs.uwaterloo.ca/~cs134/Resources/read/Articles/Bentley/Alg
orithmDesignTechniques.pdf)
Definition. Given the real vector x[ n], compute the maximum
sum found in any contiguous subvector.
Example 1:
- 6 -4 2 3 - 5 7 2 - 8 9 1
Example 2:
-2 9 -7 4 3 8 -6 1 4
Algorithm 1: The Cubic Algorithm
// A is an array that holds input vector of length N
(1) MaxSoFar  0; Low  0; High  0
(2) for L  1 to N do
(3)
for U  L to N do begin
(4)
Sum  0
(5)
for i  L to U do
(6)
Sum  Sum + Ai
(7)
if (Sum > MaxSoFar) then begin
(8)
MaxSoFar  Sum
(9)
Low  L
(10)
High  U
end
end
return(MaxSoFar, Low, High)
Algorithm 2a: First Quadratic Algorithm
Basic idea:
// A is an array that holds input vector of length N
MaxSoFar  0; Low  0; High  0
for L  1 to N do
Sum  0
for U  L to N do begin
Sum  Sum + AU // Sum contains AL + …+ AU
if (Sum > MaxSoFar) then begin
MaxSoFar  Sum
Low  L
High  U
end
end
return(MaxSoFar, Low, High)
Algorithm 2b: Another Quadratic Algorithm
Basic Idea:
CumArray[ 0 ]  0; Low  0; High  0
for i  1 to N do
CumArray[ i ]  CumArray[i-1] + Ai
MaxSoFar  0
for L  1 to N do
for U  L to N do
Sum  CumArray[U] - CumArray[L-1]
if (Sum > MaxSoFar) then begin
MaxSoFar  Sum
Low  L
High  U
end
return(MaxSoFar, Low, High)
A Divide and Conquer Algorithm
Basic Idea:
Algorithm 3 (Recursive)
Function MaxSum(L, U)
if (L = U) then
if (AL > 0) then return(AL)
else return(0)
M  (L + U)/2 // Left is AL…AM, Right is AM+1…AU
ML  MaxSum(L, M)
MR  MaxSum(M + 1, U)
Sum  0; MaxToLeft  0
for i  M downto L do begin
Sum  Sum + Ai
MaxToLeft  max(MaxToLeft, Sum)
end
Sum  0; MaxToRight  0
for i  M + 1 to U do begin
Sum  Sum + Ai
MaxToRight  max( MaxToRight, Sum)
end
MC  MaxToLeft + MaxToRight
return( max(MC, ML, MR) )
Algorithm 4
Basic idea:
MaxSoFar  0
MaxEndingHere  0
for i  1 to N do begin
MaxEndingHere  max(0, MaxEndingHere + Ai)
MaxSoFar  max(MaxSoFar, MaxEnding Here)
end
return(MaxSoFar)
Table is from Programming Pearls, Copyright 2000, Lucent Technologies
Download