From b52b8c5c7a8faad0f94e4fce63e9a07d691485ae Mon Sep 17 00:00:00 2001 From: Yang Libin Date: Wed, 2 Oct 2019 17:03:24 +0800 Subject: [PATCH] Update Interpolation Search.cpp --- Search/Interpolation Search.cpp | 49 +++++++++++++++++---------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/Search/Interpolation Search.cpp b/Search/Interpolation Search.cpp index 7a9d67a67..f52ce4f4f 100644 --- a/Search/Interpolation Search.cpp +++ b/Search/Interpolation Search.cpp @@ -1,30 +1,31 @@ -#include -int InterpolationSearch(int A[], int n, int x){ - int low =0; - int high =n-1; - while (low<=high){ - int mid = low + (((high-1)*(x-A[low]))/(A[high]-A[low])); - if(x==A[mid]) - return mid; // Found x, return (exit) - else if (x +int InterpolationSearch(int A[], int n, int x) +{ + int low = 0; + int high = n - 1; + while (low <= high) + { + int mid = low + (((high - 1) * (x - A[low])) / (A[high] - A[low])); + if (x == A[mid]) + return mid; // Found x, return (exit) + else if (x < A[mid]) + high = mid - 1; // X lies before mid + else + low = mid + 1; // x lies after mid + } + return -1; } -int main(){ - int A[] = {2,4,5,7,13,14,15,23}; - int x=17; - int index = InterpolationSearch(A, 8, x); // passed array A inside the InterpolationSearch function - if(index!= -1) - std::cout << "Number "<