/*
二分查找法
*/
#include<stdio.h>
/*
find:二分查找
@a:目标数组
@len:目标数组长度
@n:要查找的值
*/
int find(int *a,int len,int n)
{
int i,j,high,low,mid;
high=len-1;
low=0;
mid=(len-1)/2;
while(low<high)
{
if(n==a[high])
return high;
if(n==a[mid])
return mid;
if(n==a[low])
return low;
if(n>a[mid])
{
low=mid+1;
mid=(high-low)/2+low;
}
if(n<a[mid])
{
high=mid-1;
mid=mid/2;
}
}
return -1;
}
int main(int argc, char * argv [ ])
{
int a[10]={1,2,3,4,5,6,7,8,9,10},k,n;
scanf("%d",&n);
k=find(a,10,n);
if(k>=0)
{
printf("下标为%d",k);
}
else
printf("未找到该数");
}