插值查找算法

 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<math.h>

 4 #include<ctype.h>

 5 #include<stdbool.h>

 6 

 7 

 8 int Insert_search(int *a, int key, int n)

 9 {

10     int pos, low, high;

11     low = 0,high = n - 1;

12     while(low <= high){

13         pos = ((key - a[low]) * (high - low )) / (a[high] - a[low]) + low;

14         if(a[pos] < key){

15             low = pos + 1;

16         } else if(a[pos] == key){

17             return pos;

18         } else{

19             high = pos - 1;

20         }

21     }

22     return -1;

23 }

24 

25 int main()

26 {

27     

28     int a[13] = {5,15,19,20,25,31,38,41,45,49,52,55,57};

29     int k;

30     printf("请输入要查找的数字:\n");

31     scanf("%d",&k);

32     int pos = Insert_search(a,k,13);

33     if(pos != -1)

34         printf("在数组的第%d个位置找到元素:%d\n",pos + 1,k);

35     else

36         printf("未在数组中找到元素:%d\n",k);

37     return 0;

38 }

 

你可能感兴趣的:(算法)