二分查找伪代码:迭代
Binary-Search(A,low,high,x)
while(low<=high) mid=(low+high)/2 if(A[mid]==x) return mid else if (A[mid]>x) high=mid-1 else low=mid+1 return null 二分查找伪代码:递归
Binary-Search(A,low,high,x) if(low>high) return null mid=(low+high)/2 if(A[mid]==x) return mid else if (A[mid]>x) return Binary-Search(A,low,mid-1,x) else return Binary-Search(A,mid+1,high,x) 证明很简单,画树即可。T(n)=T(n/2)+O(1)
原文地址:http://zhangshifan.blog.51cto.com/9970564/1618383