#include <stdio.h>
#include <stdlib.h>
#define NUM 8
int a[NUM] = {1, 2, 4, 5 ,8, 9, 10, 15 };
int binary_search(int number)
{
int mid, start = 0, end = NUM - 1;
while(start <= end)
{
mid = (end + start) / 2;
if(a[mid] < number)
start = mid + 1;
else if(a[mid] > number)
end = mid - 1;
else
return mid;
}
return -1;
}
int main(void)
{
printf("%d\n", binary_search(8));
return 0;
}
折半查找
最新推荐文章于 2012-08-08 14:44:06 发布