Interpolation Search in C++
Search a sorted, evenly spread array with interpolation search in C++ by estimating the position from the key. Shows the formula and when it slows down.
1#include <iostream>2using namespace std;34int interpolationSearch(int arr[], int n, int key) {5 int left = 0;6 int right = n - 1;78 while (left <= right && key >= arr[left] && key <= arr[right]) {9 if (left == right) {10 if (arr[left] == key) {11 return left;12 }13 return -1;14 }1516 // Calculate position using interpolation formula17 int pos = left + ((double)(right - left) / (arr[right] - arr[left])) * (key - arr[left]);1819 if (arr[pos] == key) {20 return pos; // Found21 }2223 if (arr[pos] < key) {24 left = pos + 1; // Search right25 } else {26 right = pos - 1; // Search left27 }28 }2930 return -1; // Not found31}3233int main() {34 int arr[] = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47};35 int n = sizeof(arr) / sizeof(arr[0]);36 int key;3738 cout << "Sorted array: ";39 for (int i = 0; i < n; i++) {40 cout << arr[i] << " ";41 }42 cout << endl;4344 cout << "Enter element to search: ";45 cin >> key;4647 int result = interpolationSearch(arr, n, key);4849 if (result != -1) {50 cout << "Element found at index: " << result << endl;51 } else {52 cout << "Element not found in array" << endl;53 }5455 return 0;56}
Sample Output
Element found at index: 4
18 found on the second estimate.
In this one
- How the position formula estimates where the key should be
- Why it needs sorted, roughly uniform data
- Why the left == right case avoids division by zero
- Its O(log log n) average and O(n) worst case
How it works
- 1
left 0 (10), right 14 (47).
- 2
pos = 0 + (14 / 37) × (18 − 10) ≈ 3.
- 3
arr[3] = 16 < 18, so left = 4.
- 4
pos = 4, arr[4] = 18: found.
Edge cases
Skewed data
Search in {1, 2, 3, 4, 1000}.
The estimates land near the left end every time, so it creeps one step at a time: O(n).
All equal values
Search 5 in {5, 5, 5}.
left < right and arr[right] - arr[left] is 0, so the cast makes it (double)2 / 0 = inf, and inf × 0 is NaN. Converting NaN to int is undefined behavior. Add a check: if arr[left] == arr[right], compare directly instead of interpolating.
What's going on
The program searches for an element in a sorted array using Interpolation Search. For example:
Interpolation Search estimates the position of the key based on its value relative to the array's range, making it faster than Binary Search for uniformly distributed data.
Header File Used
#include <iostream>
This header provides:
Understanding Interpolation Search
Algorithm Concept:
Visual Example:
Array: [10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47]
Search for: 18
Step 1: left=0, right=14, arr[0]=10, arr[14]=47
Step 2: left=4, right=14
Function: interpolationSearch()
int interpolationSearch(int arr[], int n, int key) {
int left = 0;
int right = n - 1;
while (left <= right && key >= arr[left] && key <= arr[right]) {
if (left == right) {
if (arr[left] == key) {
return left;
}
return -1;
}
int pos = left + ((double)(right - left) / (arr[right] - arr[left])) * (key - arr[left]);
if (arr[pos] == key) {
return pos;
}
if (arr[pos] < key) {
left = pos + 1;
} else {
right = pos - 1;
}
}
return -1;
}
How it works:
Understanding Interpolation Formula
Formula:
pos = left + ((right - left) / (arr[right] - arr[left])) * (key - arr[left])
How it works:
Example (searching for 18):
Step-by-Step Algorithm
Step 1: Initialize Interval
Step 2: Calculate Position
Step 3: Compare
Step 4: Repeat
Time and Space Complexity
Time Complexity:
Space Complexity: O(1)
When to Use Interpolation Search
Best For:
Not Recommended For:
Comparison with Binary Search
Interpolation Search Advantages:
Interpolation Search Disadvantages:
Binary Search Advantages:
Important Considerations
Uniform Distribution Requirement:
Bounds Checking:
Position Calculation:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Worst-case time?
Pick an answer to check it.
Question 2
What must be true of the array?
Pick an answer to check it.
Questions people ask
Why the (double) cast?
To keep the fraction in the position estimate before converting back to int.
When is it best?
Large, sorted, evenly spaced data like IDs or timestamps.
How does it differ from binary search?
Binary search always picks the middle; interpolation guesses based on value.