插值查找算法

#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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值