-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpolationSearch.c
More file actions
33 lines (29 loc) · 1.04 KB
/
InterpolationSearch.c
File metadata and controls
33 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int InterpolationSearch(int* arr, int num, int l, int h)
{
int index;
while(l<=h && num>=arr[l] && num<=arr[h])
{
index = l+((num-arr[l])*(h-l))/(arr[h]-arr[l]);
if(arr[index]==num) return index;
else if(num<arr[index])
return InterpolationSearch(arr, num, l, index-1);
else
return InterpolationSearch(arr, num, index+1, h);
}
return -1;
}
int main()
{
int arr[] = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47}; //A Sorted and Uniformly Distributed Array
int n, num, index;//n = length of array, num = number to be searched, index = index at which num resides in the array(to be calculated)
n = (sizeof(arr)) / (sizeof(arr[0]));
printf("Enter the Number to be searched : ");
scanf("%d",&num);
index = InterpolationSearch (arr, num, 0, n-1);
if ( index != -1) printf("%d found at index %d",num,index);
else printf("%d not found",num);
return 0;
}