#include<stdio.h>
// 二分法查找数据
/***********************/
int search(int a[],int length,int key);
/************************/
int search(int a[],int length,int key)
{
int low,high,mid;
low = 1;
high = length;
while(low <= high)
{
mid = (low+high)/2;
if(key < a[mid-1])
high = mid -1;
else if(key > a[mid-1])
low = mid + 1;
else
return mid-1;
}
return 0;
}
/***************************/
int main()
{
int arry[10];
int length=0;
int i;
arry[0] = 1;
arry[1] = 3;
arry[2] = 8;
arry[3] = 11;
length = 5;
if(i = search(arry,length,3))
printf("the value is %d\n",i);
else
printf("Not find\n");
}
二分法查找数据
最新推荐文章于 2023-03-24 16:54:36 发布