Interpolation Search in C++
Interpolation Search Algorithm in C++ (Complete Implementation).
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}
Output
Sorted array: 10 12 13 16 18 19 20 21 22 23 24 33 35 42 47 Enter element to search: 18 Element found at index: 4
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
This header provides:
1#include <iostream>
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 -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;
}
1return left;
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.