From de2111439195f5aec77de38bf2b603a92ac2c83d Mon Sep 17 00:00:00 2001 From: walter-ind <52423075+walter-ind@users.noreply.github.com> Date: Wed, 2 Oct 2019 13:22:43 +0530 Subject: [PATCH 1/2] Add files via upload --- Search/Interpolation Search.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Search/Interpolation Search.cpp diff --git a/Search/Interpolation Search.cpp b/Search/Interpolation Search.cpp new file mode 100644 index 000000000..7a9d67a67 --- /dev/null +++ b/Search/Interpolation Search.cpp @@ -0,0 +1,30 @@ +#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 Date: Wed, 2 Oct 2019 17:03:24 +0800 Subject: [PATCH 2/2] 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 "<