Binary Search - divide the sublist into almost equal halves
BINARY_SEARCH (List, lower, upper, X)
{Look for X in List[lower, upper]. Report its position if found,
else report 0. upper >= lower > 0.}
if lower = upper then
if X = List[lower] then
return lower
else
return 0
else
mid = [(lower+upper) / 2)
if X > List[mid] then
return BINARY_SEARCH(List, mid+1, upper, X)
else
return BINARY_SEARCH(List, lower, mid, X)
- Worst Cost
- Decision Trees
Predictable (Deterministic) Algorithm: sequence of comparisons is the same for a fixed input.
- Let A be any predictable search algorithm; must have a first comparison between X and L[i] ==>: decision tree [node, children, tree, binary tree, ordered tree, root, leaf]
- level - # of nodes on the path between the root and the node
- height - level of deepest leaf
- Tree's height plus one is an upper bound on how many comparisons the algorithm must do to find X in L.
- at least n nodes in the tree
- every height m rooted binary tree has at most 2m+1 - 1 nodes [proof by induction]
- Thus 2m+1 - 1 >= n ==> m + 1 >= lg (n + 1)
m+1 >= [lg(n+1)] = [lg n] + 1
Thus BINARY SEARCH has optimal worst cost within the comparison-based model
- The Average Cost (assume that X is in L and equally likely to be anywhere in L)
- average ~ (1x1 + 2x2 + 3x4 + 4x8 + ... + lg n 2lg n - 1)/ n = 1/n (summation i2i-1) from 1 to lg n
- this is roughly n(lg n - 1) / n = lg n - 1
- f(n) = (0 if n = 0) | (1 if n = 1) | ([n/2]-1)/n*f([n/2]-1) + 1/n*0 + [n/2]/n*f([n/2])+1 if n >1
- multiply by n and replace n f(n) with g(n) ==>
g(n) = (0 if n = 0) | (1 if n = 1) | (g([n/2]-1)+g([n/2])+n
- subtract g(n) - g(n-1) and replace with h(n) ==>
h(n) = (1 if n =1) | h([n/2]) + 1 if n > 1 which is the same recurrence as the worst cost of BINARYSEARCH and can be replaced by ceil(lg(n+1)) ==> f(n) is no worse than ceil(lg(n+1)) and the average cost when X is in L is at most two comparisons less than the worst cost
- The Lower Bound on the Average Cost
- model every search algorithmin the comparison-based model by a decision tree (must have at least n nodes & every node is a possible outcome)
- the average # of comparisons = (path length / n) + 1
- Build a binary search tree (greedy stretegy) which minimizes the path length.
- minimum average height of an n-node binary tree is
ceil(lg(n+1)) - (2ceil(lg(n+1)) - ceil(lg(n+1)) - 1)/ n - 1 which is the same as the average cost of a successful BINARYSEARCH ==> optimal
- Programming - rewrite to test equality at end; tricky algorithm