From 280ddcd6f0e157f89947bd4bd942f5854e6767c7 Mon Sep 17 00:00:00 2001 From: Shardendu Kumar <72930487+shardendu930@users.noreply.github.com> Date: Sat, 15 Oct 2022 22:32:43 +0530 Subject: [PATCH] Create Interpolationsearch.cpp Added interpolation search --- C++/Interpolationsearch.cpp | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 C++/Interpolationsearch.cpp diff --git a/C++/Interpolationsearch.cpp b/C++/Interpolationsearch.cpp new file mode 100644 index 0000000..ba062a4 --- /dev/null +++ b/C++/Interpolationsearch.cpp @@ -0,0 +1,61 @@ +// C++ program to implement interpolation +// search with recursion +#include +using namespace std; + +// If x is present in arr[0..n-1], then returns +// index of it, else returns -1. +int interpolationSearch(int arr[], int lo, int hi, int x) +{ + int pos; + + // Since array is sorted, an element present + // in array must be in range defined by corner + if (lo <= hi && x >= arr[lo] && x <= arr[hi]) { + + // Probing the position with keeping + // uniform distribution in mind. + pos = lo + + (((double)(hi - lo) / (arr[hi] - arr[lo])) + * (x - arr[lo])); + + // Condition of target found + if (arr[pos] == x) + return pos; + + // If x is larger, x is in right sub array + if (arr[pos] < x) + return interpolationSearch(arr, pos + 1, hi, x); + + // If x is smaller, x is in left sub array + if (arr[pos] > x) + return interpolationSearch(arr, lo, pos - 1, x); + } + return -1; +} + +// Driver Code +int main() +{ + + // Array of items on which search will + // be conducted. + int arr[] = { 10, 12, 13, 16, 18, 19, 20, 21, + 22, 23, 24, 33, 35, 42, 47 }; + + int n = sizeof(arr) / sizeof(arr[0]); + + // Element to be searched + int x = 18; + int index = interpolationSearch(arr, 0, n - 1, x); + + // If element was found + if (index != -1) + cout << "Element found at index " << index; + else + cout << "Element not found."; + + return 0; +} + +// This code is contributed by equbalzeeshan