二分法查找
#include<iostream>using namespace std;
int HalfSearch(int arr[],int size, int key)
{
int low=0,high=size-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(key==arr[mid]);
return mid;
if(key>arr[mid])
low=mid+1;
else high=mid-1;
}
return -1;
}
int main()
{
int a[10]={13,27,36,57,65,73,82,88,90,99},value,n;
cout<<"请输入待找的整数:";
cin>>value;
if(n=HalfSearch(a,10,value)!=-1)
cout<<"找到"<<value<<"是第"<<n+1<<"个元素!\n";
else cout<<"数组中没有要找的值!\n";
return 0;
}
这道程序的二分法查找数怎么运行出来都是第二个元素啊。。是哪里错了,找不到,求解