笔试练习-数组二分法查找

#include
#include

#define LEN(x) (sizeof(x)/sizeof(int))

//默认数组data已经是从小到大排序
int bin_find(unsigned int data[],unsigned int size,unsigned int value)
{
	int mid,head,rear;

	head=0;
	mid=size/2;
	rear=size-1;

	if(value>data[size-1]||valuedata[mid])
		{
			head=mid;
			mid=(rear+head)/2;
			continue;
		}
		if(value==data[mid])
		{
			return mid;
		}
	}
}

int main(int argc, const char *argv[])
{
	unsigned int data[10]={1,2,3,4,5,6,7,8,9,11};

	printf("sizeof=%d\n",LEN(data));
	printf("b=%d\n",bin_find(data,LEN(data),2));

	return 0;
}

你可能感兴趣的:(C语言笔试题)