二分查找
#include <stdio.h>
int binary_search(int *a,int p,int q,int ele)
{
int mid=0;
if(p>q)
{
return 0;
}
mid=p+(q-p)/2;
if(ele==a[mid])
{
return mid;
}
if(ele<a[mid])
{
return binary_search(a,p,mid-1,ele);
}
else
{
return binary_search(a,mid+1,q,ele);
}
}
int main()
{
int a[10]={10,14,19,26,27,31,33,35,42,44};
printf("%d\n",binary_search(a,0,9,31));
return 0;
}
标签:二分,return,int,mid,ele,查找
From: https://www.cnblogs.com/yesiming/p/17452849.html