作者:农村的企业家 | 来源:互联网 | 2023-10-12 14:43
二分查找伪代码:迭代
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)
算法导论第三版 练习2.3-5