插值查找算法

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdbool.h>


int Insert_search(int *a, int key, int n)
{
    int pos, low, high;
    low = 0,high = n - 1;
    while(low <= high){
        pos = ((key - a[low]) * (high - low )) / (a[high] - a[low]) + low;
        if(a[pos] < key){
            low = pos + 1;
        } else if(a[pos] == key){
            return pos;
        } else{
            high = pos - 1;
        }
    }
    return -1;
}

int main()
{
    
    int a[13] = {5,15,19,20,25,31,38,41,45,49,52,55,57};
    int k;
    printf("请输入要查找的数字:\n");
    scanf("%d",&k);
    int pos = Insert_search(a,k,13);
    if(pos != -1)
        printf("在数组的第%d个位置找到元素:%d\n",pos + 1,k);
    else
        printf("未在数组中找到元素:%d\n",k);
    return 0;
}

 

你可能感兴趣的:(c,算法,插值查找算法)