#include <stdio.h>
int search(int *var, int target, int low, int high)
{
int mid = (low + high)/2;
while (low < high)
{
if (target > var[mid])
{
return search(var, target, mid + 1, high);
}
else if (target < var[mid])
{
return search(var, target, low, mid - 1);
}
else
{
printf("The firt matching position is: %d \n", mid);
return mid;
}
}
return -1;
}
int main()
{
int array[20] = {2, 12, 78, 98, 245, 320, 417, 578, 643, 708, 923, 978, 996, 1002, 1200, 2300, 2398, 2400, 2521, 2888};
int target = 23;
int length = sizeof(array)/sizeof(int);
if (search(array, target, 0, length) == -1)
{
printf("Can not find the target. \n");
}
}
来源:CSDN
作者:一只叫可乐的金毛
链接:https://blog.csdn.net/m0_37051568/article/details/104381262