int half_find1(int* a, int high, int low,int key)
{
while (low<=high)
{
int mid = (high + low) / 2;
if (a[mid] == key)
{
return mid;
}
else if (a[mid] > key)
{
high = mid-1;
}
else
{
low = mid+1;
}
}
return -1;
}
int half_find2(int* a, int high, int low, int key)
{
int avg = (high + low) / 2;
if (a[avg] == key)
{
return avg;
}
else if (high > low)
{
return a[avg] > key ? half_find2(a, avg - 1, low, key) : half_find2(a, high, avg + 1, key);
}
else
{
return -1;
}
}
int main()
{
//二分查找
int a[] = {1,6,7,12,24,26,30,45,123};
int high = sizeof(a) / sizeof(a[0])-1;
int low = 0;
int index1=half_find1(a,high,low,5);
int index2=half_find2(a,high,low,45);
//printf("%d\n", index1);
printf("%d\n", index2);
return 0;
}