-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterpolationSearch.cpp
More file actions
62 lines (54 loc) · 1.33 KB
/
Copy pathinterpolationSearch.cpp
File metadata and controls
62 lines (54 loc) · 1.33 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
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
#include <iostream>
using namespace std;
int interpolationSearch(int arr[], int t, int n)
{
int start, end, pos;
start = 0;
end = t - 1;
while (start<=end&&n>=arr[start]&&n<=arr[end])
{
pos = start + (((double)(end-start)/(arr[end]-arr[start])) * (n - arr[start]));
if (arr[pos]==n)
{
return pos;
}
else if(n>arr[pos])
{
start = pos + 1;
}
else {
end = pos - 1;
}
}
return -1;
}
int main()
{
int arr[5] = {1,3,5,4,2};
int temp, e;
for (int i=0; i<5; i++)
{
cout << arr[i] << endl;
}
for (int i=0; i<=5; i++)
{
for (int j=0; j<=5; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1]= temp;
}
}
}
cout << "array after sorting: " << endl;
for (int i=0; i<5; i++)
{
cout << arr[i] << endl;
}
cout << "Enter the number you want to find: " << endl;
cin >> e;
cout << "The entered number is at index: " << interpolationSearch(arr,4,e) << endl;
return 0;
}