-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interpolation_Search.cpp
55 lines (49 loc) · 1.09 KB
/
Interpolation_Search.cpp
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
///Time Complexity: O(log2(log2 n)) for the average case
/*
Auther : Abdullah Al Masum
*/
#include <bits/stdc++.h>
#include <stdio.h>
#define maximum 100
using namespace std;
int interpolationSearch(int A[], int n, int e)
{
int start, end, pos;
start = 0;
end = n - 1;
while (start <= end && e >= A[start] && e <= A[end])
{
pos = start + (((double)(end - start) / (A[end] - A[start])) * (e - A[start]));
if (A[pos] == e)
return pos;
if (e > A[pos])
start = pos + 1;
else
end = pos - 1;
}
return -1;
}
int main()
{
int len, search_element;
cout << "input the length : ";
cin >> len;
int A[len];
cout << "input the values : " << endl;
for (int i = 0; i < len; i++)
{
cin >> A[i];
}
cout << "input the search element : " << endl;
cin >> search_element;
int index = interpolationSearch(A, len, search_element);
if (index != -1)
{
cout << "search position : " << index;
}
else
{
cout << "not found" << endl;
}
return 0;
}