2. Introduction
Interpolation search is an improved variant of binary search. This search algorithm works on
the probing position of the required value. For this algorithm to work properly, the data
collection should be in a sorted form and equally distributed.
Binary search has a huge advantage of time complexity over linear search. Linear search has
worst-case complexity of Ο(n) whereas binary search has Ο(log n).
There are cases where the location of target data may be known in advance. For example, in
case of a telephone directory, if we want to search the telephone number of Morpheus. Here,
linear search and even binary search will seem slow as we can directly jump to memory space
where the names start from 'M' are stored.
3. Positioning in Binary Search
In binary search, if the desired data is not found then the rest of the list is divided in two parts,
lower and higher. The search is carried out in either of them.
4. Even when the data is sorted, binary search does not take advantage to probe the position of
the desired data.
Position Probing in Interpolation Search
Interpolation search finds a particular item by computing the probe position. Initially, the probe
position is the position of the middle most item of the collection.
5. If a match occurs, then the index of the item is returned. To split the list into
two parts, we use the following method −
mid = Lo + ((Hi - Lo) / (A[Hi] - A[Lo])) * (X - A[Lo])
where −
A = list
Lo = Lowest index of the list
Hi = Highest index of the list
A[n] = Value stored at index n in the list
If the middle item is greater than the item, then the probe position is again calculated in the
sub-array to the right of the middle item. Otherwise, the item is searched in the subarray to
the left of the middle item. This process continues on the sub-array as well until the size of
subarray reduces to zero. Runtime complexity of interpolation search algorithm is Ο(log
(log n)) as compared to Ο(log n) of BST in favorable situations.
6. Interpolation Search
Given a sorted array of n uniformly distributed values array[], write a function to
search for a particular element x in the array.
Linear Search finds the element in O(n) time, Jump search takes O(√ n) time
and Binary search take O(Log n) time.
7. The Interpolation Search is an improvement over Binary Search for
instances, where the values in a sorted array are uniformly distributed.
Binary Search always goes to the middle element to check. On the
other hand, interpolation search may go to different locations
according to the value of the key being searched. For example, if the
value of the key is closer to the last element, interpolation search is
likely to start search toward the end side.
8. // The idea of formula is to return higher value of pos
// when element to be searched is closer to array[hi]. And
// smaller value when closer to array[lo]
pos = lo + [ (x-array[lo])*(hi-lo) / (array[hi]-array[Lo]) ]
array[] ==> Array where elements need to be searched
x ==> Element to be searched
lo ==> Starting index in array[]
hi ==> Ending index in array[]
To find the position to be searched, it uses following formula.
9. Let's assume that the elements of the array are linearly
distributed.
General equation of line : y = m*x + c.
y is the value in the array and x is its index.
Now putting value of lo , hi and x in the equation
array[hi] = m * hi + c ----(1)
Array[lo] = m * lo + c ----(2)
x = m*pos + c ----(3)
m = (array[hi] - array[lo] )/ (hi - lo)
subtracting equation (2) from (3)
x - array[lo] = m * (pos - lo) lo + (x - array[lo])/m = pos
pos = lo + (x - array[lo]) *(hi - lo)/(array[hi] - array[lo])
10. Algorithm
Rest of the Interpolation algorithm is the same except the above partition logic.
Step1: In a loop, calculate the value of “pos” using the probe position formula.
Step2: If it is a match, return the index of the item, and exit.
Step3: If the item is less than array[pos], calculate the probe position of the left sub-array.
Otherwise calculate the same in the right sub-array.
Step4: Repeat until a match is found or the sub-array reduces to zero.
Below is the implementation of algorithm.
12. # Python3 program to implement
# interpolation search
# with recursion
# If x is present in array[0..n-1], then
# returns index of it, else returns -1.
def interpolationSearch(array, lo, hi, x):
# Since array is sorted, an element present
# in array must be in range defined by corner
if (lo <= hi and x >= array[lo] and x <= array[hi]):
# Probing the position with keeping
# uniform distribution in mind.
pos = lo + ((hi - lo) // (array[hi] - array[lo]) *(x - array[lo]))
# Condition of target found
if array[pos] == x:
return pos
# If x is larger, x is in right subarray
if array[pos] < x:
return interpolationSearch(array, pos + 1,hi, x)
# If x is smaller, x is in left subarray
if array[pos] > x:
return interpolationSearch(array, lo, pos - 1, x)
return -1
13. # Driver code
# Array of items in which
# search will be conducted
array = [10, 12, 13, 16, 18, 19, 20,
21, 22, 23, 24, 33, 35, 42, 47]
n = len(array)
# Element to be searched
x = 18
index = interpolationSearch(array, 0, n - 1, x)
if index != -1:
print("Element found at index", index)
else:
print("Element not found")
Output
Element found at index 4