二分(更新中)

int BinarySearch(int *a,int &e,int n)//在递增序数组a中找x
{
int left=0;
int right=n-1;
while(left<=right)//<=
{
int mid=(left+right)/2;
if(e>a[mid]) left=mid+1;
else
if(e<a[mid]) right=mid-1;
else
if(e==a[mid]) return mid;
}
return -1;//未找到e
}

posted @ 2012-04-27 00:41  索少  阅读(136)  评论(0编辑  收藏  举报