|
| 1 | +--- |
| 2 | +id: Interpolation-Search |
| 3 | +title: Interpolation Search (Geeks for Geeks) |
| 4 | +sidebar_label: Interpolation Search |
| 5 | +tags: |
| 6 | + - Intermediate |
| 7 | + - Search Algorithms |
| 8 | + - Geeks for Geeks |
| 9 | + - CPP |
| 10 | + - Python |
| 11 | + - Java |
| 12 | + - JavaScript |
| 13 | + - DSA |
| 14 | +description: "This is a solution to the Interpolation Search problem." |
| 15 | +--- |
| 16 | + |
| 17 | +## What is Interpolation Search? |
| 18 | + |
| 19 | +Interpolation Search is an efficient search algorithm for uniformly distributed sorted arrays. It works by estimating the position of the target value based on the value's distribution, making it faster than linear search and in some cases more efficient than binary search. |
| 20 | + |
| 21 | +## Algorithm for Interpolation Search |
| 22 | + |
| 23 | +1. Initialize the low and high indices to 0 and N-1, respectively. |
| 24 | +2. While the target value is within the range defined by the current low and high indices: |
| 25 | + - Calculate the probe position using the formula: |
| 26 | + $$ |
| 27 | + \text{pos} = \text{low} + \left( \frac{(x - \text{arr}[low]) \times (\text{high} - \text{low})}{\text{arr}[high] - \text{arr}[low]} \right) |
| 28 | + $$ |
| 29 | +3. Check the value at the probe position: |
| 30 | + - If `arr[pos]` is equal to the target value, return `pos`. |
| 31 | + - If `arr[pos]` is less than the target value, update `low` to `pos + 1`. |
| 32 | + - If `arr[pos]` is greater than the target value, update `high` to `pos - 1`. |
| 33 | +4. If the target value is not found, return -1. |
| 34 | + |
| 35 | +## How does Interpolation Search work? |
| 36 | + |
| 37 | +- It calculates a probe position using a formula that considers the distribution of values within the array. |
| 38 | +- The probe position is used to narrow down the search range, making the search process more efficient compared to a linear search. |
| 39 | + |
| 40 | +## Problem Description |
| 41 | + |
| 42 | +Given a sorted list and a target element, implement the Interpolation Search algorithm to find the index of the target element in the list. If the element is not present, return -1. |
| 43 | + |
| 44 | +## Examples |
| 45 | + |
| 46 | +**Example 1:** |
| 47 | +Input: |
| 48 | +list = [10, 12, 13, 16, 18, 19, 20, 21, 22, 23] |
| 49 | +target = 18 |
| 50 | +Output: 4 |
| 51 | + |
| 52 | + |
| 53 | +**Example 2:** |
| 54 | +Input: |
| 55 | +list = [10, 12, 13, 16, 18, 19, 20, 21, 22, 23] |
| 56 | +target = 25 |
| 57 | +Output: -1 |
| 58 | + |
| 59 | + |
| 60 | +## Your Task: |
| 61 | + |
| 62 | +You don't need to read input or print anything. Complete the function interpolation_search() which takes arr[], N and K as input parameters and returns the index of K in the array. If K is not present in the array, return -1. |
| 63 | + |
| 64 | +Expected Time Complexity: $O(\log \log N)$ |
| 65 | +Expected Auxiliary Space: $O(1)$ |
| 66 | + |
| 67 | +## Constraints |
| 68 | + |
| 69 | +- $1 <= N <= 10^5$ |
| 70 | +- $1 <= arr[i] <= 10^6$ |
| 71 | +- $1 <= K <= 10^6$ |
| 72 | + |
| 73 | +## Implementation |
| 74 | + |
| 75 | +<Tabs> |
| 76 | + <TabItem value="Python" label="Python" default> |
| 77 | + |
| 78 | +```python |
| 79 | + |
| 80 | +def interpolation_search(arr, n, x): |
| 81 | + low = 0 |
| 82 | + high = n - 1 |
| 83 | + |
| 84 | + while low <= high and x >= arr[low] and x <= arr[high]: |
| 85 | + if low == high: |
| 86 | + if arr[low] == x: |
| 87 | + return low |
| 88 | + return -1 |
| 89 | + |
| 90 | + pos = low + ((x - arr[low]) * (high - low) // (arr[high] - arr[low])) |
| 91 | + |
| 92 | + if arr[pos] == x: |
| 93 | + return pos |
| 94 | + if arr[pos] < x: |
| 95 | + low = pos + 1 |
| 96 | + else: |
| 97 | + high = pos - 1 |
| 98 | + return -1 |
| 99 | +``` |
| 100 | +</TabItem> |
| 101 | + <TabItem value="C++" label="C++"> |
| 102 | + |
| 103 | +```cpp |
| 104 | + |
| 105 | +#include <iostream> |
| 106 | +#include <vector> |
| 107 | + |
| 108 | +int interpolation_search(const std::vector<int>& arr, int n, int x) { |
| 109 | + int low = 0, high = n - 1; |
| 110 | + |
| 111 | + while (low <= high && x >= arr[low] && x <= arr[high]) { |
| 112 | + if (low == high) { |
| 113 | + if (arr[low] == x) return low; |
| 114 | + return -1; |
| 115 | + } |
| 116 | + |
| 117 | + int pos = low + ((x - arr[low]) * (high - low) / (arr[high] - arr[low])); |
| 118 | + |
| 119 | + if (arr[pos] == x) |
| 120 | + return pos; |
| 121 | + if (arr[pos] < x) |
| 122 | + low = pos + 1; |
| 123 | + else |
| 124 | + high = pos - 1; |
| 125 | + } |
| 126 | + return -1; |
| 127 | +} |
| 128 | + |
| 129 | +int main() { |
| 130 | + std::vector<int> arr = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23}; |
| 131 | + int target = 18; |
| 132 | + std::cout << "Index: " << interpolation_search(arr, arr.size(), target) << std::endl; |
| 133 | + return 0; |
| 134 | +} |
| 135 | +``` |
| 136 | +
|
| 137 | +</TabItem> |
| 138 | +<TabItem value="Java" label="Java"> |
| 139 | +
|
| 140 | +```java |
| 141 | +
|
| 142 | +public class InterpolationSearch { |
| 143 | + public static int interpolationSearch(int[] arr, int n, int x) { |
| 144 | + int low = 0, high = n - 1; |
| 145 | +
|
| 146 | + while (low <= high && x >= arr[low] && x <= arr[high]) { |
| 147 | + if (low == high) { |
| 148 | + if (arr[low] == x) return low; |
| 149 | + return -1; |
| 150 | + } |
| 151 | +
|
| 152 | + int pos = low + ((x - arr[low]) * (high - low) / (arr[high] - arr[low])); |
| 153 | +
|
| 154 | + if (arr[pos] == x) |
| 155 | + return pos; |
| 156 | + if (arr[pos] < x) |
| 157 | + low = pos + 1; |
| 158 | + else |
| 159 | + high = pos - 1; |
| 160 | + } |
| 161 | + return -1; |
| 162 | + } |
| 163 | +
|
| 164 | + public static void main(String[] args) { |
| 165 | + int[] arr = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23}; |
| 166 | + int target = 18; |
| 167 | + System.out.println("Index: " + interpolationSearch(arr, arr.length, target)); |
| 168 | + } |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +</TabItem> |
| 173 | + <TabItem value="JavaScript" label="JavaScript"> |
| 174 | + |
| 175 | +```javascript |
| 176 | + |
| 177 | +function interpolationSearch(arr, n, x) { |
| 178 | + let low = 0, high = n - 1; |
| 179 | + |
| 180 | + while (low <= high && x >= arr[low] && x <= arr[high]) { |
| 181 | + if (low === high) { |
| 182 | + if (arr[low] === x) return low; |
| 183 | + return -1; |
| 184 | + } |
| 185 | + |
| 186 | + let pos = low + Math.floor(((x - arr[low]) * (high - low) / (arr[high] - arr[low]))); |
| 187 | + |
| 188 | + if (arr[pos] === x) |
| 189 | + return pos; |
| 190 | + if (arr[pos] < x) |
| 191 | + low = pos + 1; |
| 192 | + else |
| 193 | + high = pos - 1; |
| 194 | + } |
| 195 | + return -1; |
| 196 | +} |
| 197 | + |
| 198 | +const arr = [10, 12, 13, 16, 18, 19, 20, 21, 22, 23]; |
| 199 | +const target = 18; |
| 200 | +console.log("Index:", interpolationSearch(arr, arr.length, target)); |
| 201 | +``` |
| 202 | + |
| 203 | +</TabItem> |
| 204 | +</Tabs> |
| 205 | + |
| 206 | +# Complexity Analysis |
| 207 | +## Time Complexity: |
| 208 | +$O(\log \log n)$ for uniformly distributed data, where $n$ is the number of elements in the list. |
| 209 | +## Space Complexity: |
| 210 | +$O(1)$, as no extra space is required apart from the input list. |
| 211 | + |
| 212 | +# Advantages and Disadvantages |
| 213 | +## Advantages: |
| 214 | + |
| 215 | +Faster than linear search and binary search for uniformly distributed sorted lists. |
| 216 | + |
| 217 | +Efficient for large datasets. |
| 218 | + |
| 219 | +## Disadvantages: |
| 220 | + |
| 221 | +Requires the list to be sorted. |
| 222 | + |
| 223 | +Performance degrades if the distribution of elements is not uniform. |
0 commit comments