Posted in 面试题 onMay 13, 2013
1)递归方法实现:
int BSearch(elemtype a[],elemtype x,int low,int high)
/*在下届为low,上界为high的数组a中折半查找数据元素x*/
{
int mid;
if(low>high) return -1;
mid=(low+high)/2;
if(x==a[mid]) return mid;
if(x else return(BSearch(a,x,mid+1,high));
}
2)非递归方法实现:
int BSearch(elemtype a[],keytype key,int n)
{
int low,high,mid;
low=0;high=n-1;
while(low {
mid=(low+high)/2;
if(a[mid].key==key) return mid;
else if(a[mid].key
else high=mid-1;
}
return -1;
}
int BSearch(elemtype a[],elemtype x,int low,int high)
/*在下届为low,上界为high的数组a中折半查找数据元素x*/
{
int mid;
if(low>high) return -1;
mid=(low+high)/2;
if(x==a[mid]) return mid;
if(x else return(BSearch(a,x,mid+1,high));
}
2)非递归方法实现:
int BSearch(elemtype a[],keytype key,int n)
{
int low,high,mid;
low=0;high=n-1;
while(low {
mid=(low+high)/2;
if(a[mid].key==key) return mid;
else if(a[mid].key
}
return -1;
}
写出二分查找算法的两种实现
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@