#include <bits/stdc++.h>
using namespace std;
void interpolationSearch(int *a, int n, int data)
{
int low = 0, k, high = n - 1;
while (low <= high)
{
k = low + (((data - a[low]) * (high - low)) / (a[high] - a[low]));
if (data == a[k])
{
cout << "data found" << endl;
return;
}
if (data < a[k])
high = k - 1;
else
low = k + 1;
}
cout << "data not found" << endl;
return;
}
int main()
{
int n, data;
// enter array length
cout << "enter size of data\n";
cin >> n;
int *a = (int *)(malloc(sizeof(int) * n));
// fill the array
cout << "enter data\n";
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
cout << "enter element to be searched" << endl;
cin >> data;
interpolationSearch(a, n, data);
return 0;
}
Output
enter size of data
5
enter data
10
12
14
16
17
enter element to be searched
14
data found
0 Comments