搜索过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜索过程结束;如果某一特定元素大于或者小于中间元素,则在数组大于或小于中间元素的那一半中查找,而且跟开始一样从中间元素开始比较。如果在某一步骤数组为空,则代表找不到。这种搜索算法每一次比较都使搜索范围缩小一半
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9};
int length = sizeof(array)/sizeof(array[0]);//用sizeof()获取数组的长度
int element;
scanf("%d",&element);
int location = binary_research(array,length,element);
if(location>=0)
printf("The element's location is %d\n",location);
else
printf("Don't exit this element.\n");
return 0;
}
//在有序表R[0..n-1]中进行二分查找,成功时返回结点的位置,失败时返回-1
int binary_research(int arr[],int length,int element)
{
int left=0,right=length-1; //置当前查找区间上、下界的初值
while(left<=right)
{
int mid = (left+right)/2;//会有整数溢出的问题,具体见函数说明
if(arr[mid]>element)
right = mid - 1; //继续在R[left..mid-1]中查找
else if(arr[mid]<element)
left = mid + 1; //继续在R[mid+1..right]中查找
else
return mid; //查找成功返回
}
return -1; //当 left>right 时表示所查找区间内没有结果,查找失败
}
因此改进后的程序为:
int binary_research(int array[],int length,int target)
{
int low=0,high=length-1;
while(low<=high)
{
int mid=low+(high-low)/2;//优化溢出问题
if(array[mid]>target)
high=mid-1;
else if(array[mid]<target)
low=mid+1;
else
return mid;
}
return-1;
}
另外一种写法:int binary_research(int arr[],int low,int high,int target);
#include
int binary_research(int arr[],int low,int high,int target);
int main()
{
int array[] = {1,2,3,4,5,6,7,8,9};//数组中的数(由小到大)
int length = sizeof(array)/sizeof(array[0]);//用sizeof()获取数组的长度
int low=0,high=length-1;
int element;
scanf("%d",&element);
int location = binary_research(array,low,high,element);
if(location>=0)
printf("The element's location is %d\n",location);
else
printf("Don't exit this element.\n");
return 0;
}
int binary_research(int array[],int low,int high,int target)
{
while(low<=high)
{
int mid=low+(high-low)/2;//优化溢出问题
if(array[mid]>target)
high=mid-1;
else if(array[mid]<target)
low=mid+1;
else
return mid;
}
return-1;
}
除此以外还可以使用 递归函数 实现折半查找:
//递归函数实现
int binary_research(int array[],int low,int high,int target)
{
int mid=low+(high-low)/2;//优化溢出问题
if(low>high)//查找完毕没有找到答案,返回-1
return -1;
else
{
if(array[mid]==target)
return mid;//找到!返回位置.
else if(array[mid]>target)
return binary_research(array,low,mid-1,target);//找左边
else
return binary_research(array,mid+1,high,target);//找右边
}
}
参考文章:
1.https://blog.csdn.net/qq_31828515/article/details/51791833
2.https://baike.baidu.com/item/二分查找/10628618?fr=aladdin