From 4b7156d0926d1407151d451514987fa8fac561de Mon Sep 17 00:00:00 2001 From: i-vishi Date: Sun, 7 Oct 2018 01:50:01 +0530 Subject: [PATCH] Create interpolationSearch.java --- .../java/interpolationSearch.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Searching/Interpolation Search/java/interpolationSearch.java diff --git a/Searching/Interpolation Search/java/interpolationSearch.java b/Searching/Interpolation Search/java/interpolationSearch.java new file mode 100644 index 000000000..ce2fb7ae6 --- /dev/null +++ b/Searching/Interpolation Search/java/interpolationSearch.java @@ -0,0 +1,34 @@ +class Test +{ + static int arr[] = new int[]{10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47}; + static int interpolationSearch(int x) + { + int lo = 0, hi = (arr.length - 1); + while (lo <= hi && x >= arr[lo] && x <= arr[hi]) + { + + int pos = lo + (((hi-lo) / + (arr[hi]-arr[lo]))*(x - arr[lo])); + + if (arr[pos] == x) + return pos; + if (arr[pos] < x) + lo = pos + 1; + + else + hi = pos - 1; + } + return -1; + } + + // Driver method + public static void main(String[] args) + { + int x = 18; // Element to be searched + int index = interpolationSearch(x); + if (index != -1) + System.out.println("Element found at index " + index); + else + System.out.println("Element not found."); + } +}