forked from kelvins/algorithms-and-data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterpolationSearch.cpp
65 lines (51 loc) · 1.4 KB
/
InterpolationSearch.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
56
57
58
59
60
61
62
63
64
65
//Time Complexity: O(log(logn))
#include<iostream>
#include<algorithm>
using namespace std;
// If x is present in arr[0..n-1], then returns
// index of it, else returns -1.
long long int interpolation_search(int arr[],int n,int x)
{
int low=0,high=n-1;
//Beacuse the array is sorted, element has to be present in range
//made by corner i.e low and high
while ((low<=high) and (x>=arr[low]) and (x<=arr[high]))
{
// Now we will enquire the position keeping in mind the uniform distribution in mind
int pos=low+(((double)(high-low)/(arr[high]-arr[low]))*(x-arr[low]));
// If x is larger, x is in upper part
if (arr[pos]<x)
low=pos+1;
// If x is smaller, x is in lower part
else if(arr[pos]>x)
high=pos-1;
// Target found
else
return pos;
}
return -1;
}
int main()
{
int n;
cout<<"Please enter the number of elements in array\n";
cin>>n;
int arr[n];
cout<<"Please enter "<<n<<" numbers for arrays\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
// For Interpolation search we need to sort the array
sort(arr,arr+n);
cout<<"Enter the number you want to search\n";
int x;// Target number
cin>>x;
int index=interpolation_search(arr, n, x);
// If element was found
if (index != -1)
cout<<"Element found at index "<<index<<" in sorted array\n";
else
cout<<"Element not found.\n";
return 0;
}