# 简单查找算法之折半查找、插值查找、斐波那契查找

int binary_find(int arr[],int key,int n)
{
int high,mid,low;
high=n-1;
low=0;
while(low<=high)
{
mid=(low+high)/2;
if(key<arr[mid])
high=mid-1;
else if(key>arr[mid])
low=mid+1;
else if(key==arr[mid])
return mid;
else
return -1;
}
}

int insertvalue_find(int arr[],int key,int n)
{
int high,mid,low;
high=n-1;
low=0;
while(low<=high)
{
mid=low+(high-low)*(key-arr[low])/(arr[high]-arr[low]);
if(key<arr[mid])
high=mid-1;
else if(key>arr[mid])
low=mid+1;
else if(key==arr[mid])
return mid;
else
return -1;
}
}

int fabonacci(int n)
{
int result;
int p_result;
int n_result;

result = p_result = 1;

while(n>2)
{
n=n-1;
n_result = p_result;
p_result = result;
result = p_result + n_result;
}
return result;
}

int Fabonacci_find(int arr[],int key,int n)
{
int low,high,mid,i,k;
int *F;
low=0;
high=n-1;
k=0;
F=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++){
F[i]=fabonacci(i);
//printf("%d\t",F[i]);
}
//printf("\n");
while((n-1)>(F[k]-1))
k++;
for(i=n-1;i<F[k]-1;i++)
arr[i]=arr[n-1];

while(low<=high)
{
mid=low+F[k-1]-1;

if(key<arr[mid])
{
high=mid-1;
k=k-1;
}
else if(key>arr[mid])
{
low=mid+1;
k=k-2;
}
else
{
if(mid<=n-1)
{
free(F);
return mid;
}
else
{
free(F);
return n-1;
}
}
}
free(F);return 0;
}

04-07 5663

07-09 851
01-22 122
01-02 659
12-24 1200
01-20 1080
12-21 116
06-24 47
07-16 3693
09-03 117
02-01 210