diff --git a/searching/InterpolationSearch.cpp b/searching/InterpolationSearch.cpp new file mode 100644 index 0000000..dec0bff --- /dev/null +++ b/searching/InterpolationSearch.cpp @@ -0,0 +1,33 @@ +// C program to implement interpolation search +#include + +// If x is present in arr[0..n-1], then returns +// index of it, else returns -1. +int interpolationSearch(int arr[], int n, int x) +{ + // Find indexes of two corners + int lo = 0, hi = (n - 1); + + // Since array is sorted, an element present + // in array must be in range defined by corner + while (lo <= hi && x >= arr[lo] && x <= arr[hi]) + { + // Probing the position with keeping + // uniform distribution in mind. + int 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 upper part + if (arr[pos] < x) + lo = pos + 1; + + // If x is smaller, x is in the lower part + else + hi = pos - 1; + } + return -1; +}