-
Notifications
You must be signed in to change notification settings - Fork 367
/
interpolation_search.cpp
74 lines (58 loc) · 2.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
/*
----------------------
problem - Given a sorted integer array and a target,
determine if the target exists in the array or not using an
interpolation search algorithm. If the target exists in the array, return the index of it.
---------------
it is same as binary search but it use this formula to calculate mid -
mid = low + ((target – A[low]) * (high – low) / (A[high] – A[low]));
--------------------
variables -
start - starting index also know as low or low (low most element )
end - ending index also know as high or right ( right most element)
target - element to be search in the array
size - size of the array
---------------------
*/
//Function to determine if target exists in a sorted array `array` or not
// using an interpolation search algorithm
int interpolationSearch(int array[], int start, int end, int target)
{
int mid;
// search space is array[low…high]
if (start <= end && target >= array[start] && target <= array[end]) {
// estimate mid
mid = start+ (((long long)(end - start) / (array[end] - array[start]))
* (target - array[start]));
//target value found
if (array[mid] == target)
return mid;
//target is big then discard all elements in the left search space, including the middle element
if (array[mid] < target)
return interpolationSearch(array, mid + 1, end, target);
//target is small then discard all elements in the right search space, including the middle elemen
if (array[mid] > target)
return interpolationSearch(array, start, mid - 1, target);
}
//if target is not found
return -1;
}
// Driver Code
int main()
{
int array[] = {2, 5, 10, 14, 21, 26, 32, 37, 42, 49};
int size = sizeof(array) / sizeof(array[0]);
//value you want to search in the array
int target = 18;
//function call for inpterpolationSearch
int index = interpolationSearch(array, 0, size - 1, target);
// If element was found
if (index != -1)
cout << "Element found at index " << index;
else
cout << "Element not found.";
return 0;
}
// code is contributed by rajendra mandliya