From 6a054a6c5b97796da89ddcb77ab807962f63e660 Mon Sep 17 00:00:00 2001 From: mary1715 Date: Tue, 13 Jan 2026 21:06:03 +0200 Subject: [PATCH 1/2] add Interpolation Search --- Searching/InterpolationSearch.java | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Searching/InterpolationSearch.java diff --git a/Searching/InterpolationSearch.java b/Searching/InterpolationSearch.java new file mode 100644 index 00000000..d3a27950 --- /dev/null +++ b/Searching/InterpolationSearch.java @@ -0,0 +1,43 @@ +public class InterpolationSearch { + + /** + * Performs Interpolation Search on a sorted array. + * + * @param arr Sorted array of integers + * @param key Element to search + * @return index of key if found, otherwise -1 + */ + public static int interpolationSearch(int[] arr, int key) { + + int low = 0; + int high = arr.length - 1; + + while (low <= high && key >= arr[low] && key <= arr[high]) { + + // Avoid division by zero + if (arr[high] == arr[low]) { + if (arr[low] == key) { + return low; + } else { + return -1; + } + } + + // Estimate the position + int pos = low + ((key - arr[low]) * (high - low)) + / (arr[high] - arr[low]); + + if (arr[pos] == key) { + return pos; + } + + if (arr[pos] < key) { + low = pos + 1; + } else { + high = pos - 1; + } + } + + return -1; + } +} From b600b0e27ff10632eec8d16082ecb0e08ab7a54e Mon Sep 17 00:00:00 2001 From: mary1715 Date: Tue, 13 Jan 2026 21:07:43 +0200 Subject: [PATCH 2/2] add Interpolation Search --- Searching/InterpolationSearch.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Searching/InterpolationSearch.java b/Searching/InterpolationSearch.java index d3a27950..0679e629 100644 --- a/Searching/InterpolationSearch.java +++ b/Searching/InterpolationSearch.java @@ -1,5 +1,4 @@ public class InterpolationSearch { - /** * Performs Interpolation Search on a sorted array. *