0% found this document useful (0 votes)
4 views

16. Data Structure - Interpolation Search

Interpolation search is an enhanced version of binary search that uses a probing position to find the target value, requiring the data to be sorted and uniformly distributed. It has a time complexity of O(log(log n)), which is more efficient than binary search's O(log n) in favorable scenarios. The algorithm involves calculating a probe position based on the distribution of values and recursively searching in the appropriate sub-array until the target is found or the sub-array size reduces to zero.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

16. Data Structure - Interpolation Search

Interpolation search is an enhanced version of binary search that uses a probing position to find the target value, requiring the data to be sorted and uniformly distributed. It has a time complexity of O(log(log n)), which is more efficient than binary search's O(log n) in favorable scenarios. The algorithm involves calculating a probe position based on the distribution of values and recursively searching in the appropriate sub-array until the target is found or the sub-array size reduces to zero.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Structure -

Interpolation Search
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 Morphius. 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.

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.
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.
Algorithm
As it is an improvisation of the existing BST algorithm, we are mentioning the
steps to search the 'target' data value index, using position probing −
Step 1 − Start searching data from middle of the list.
Step 2 − If it is a match, return the index of the item, and exit.
Step 3 − If it is not a match, probe position.
Step 4 − Divide the list using probing formula and find the new middle.
Step 5 − If data is greater than middle, search in higher sub-list.
Step 6 − If data is smaller than middle, search in lower sub-list.
Step 7 − Repeat until match.
// The idea of formula is to return higher value of pos
// when element to be searched is closer to arr[hi].
// smaller value when closer to arr[lo]
pos = lo + [ (x-arr[lo])*(hi-lo) / (arr[hi]-arr[Lo]) ]
arr[] ==> Array where elements need to be searched x ==> Element to be searched
lo ==> Starting index in arr[]
hi ==> Ending index in arr[]

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
arr[hi] = m*hi+c ----(1)
arr[lo] = m*lo+c ----(2)
x = m*pos + c ----(3)
m = (arr[hi] - arr[lo] )/ (hi - lo)
subtracting eqxn (2) from (3)
x - arr[lo] = m * (pos - lo)
lo + (x - arr[lo])/m = pos
pos = lo + (x - arr[lo]) *(hi - lo)/(arr[hi] - arr[lo])
Another way : 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 arr[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.
# Python3 program to implement

# interpolation search

# with recursion

# If x is present in arr[0..n-1], then

# returns index of it, else returns -1.

def interpolationSearch(arr, lo, hi, x):

# Since array is sorted, an element present

# in array must be in range defined by corner

if (lo <= hi and x >= arr[lo] and x <= arr[hi]):

# Probing the position with keeping

# uniform distribution in mind.

pos = lo + ((hi - lo) // (arr[hi] - arr[lo]) *

(x - arr[lo]))

# Condition of target found

if arr[pos] == x:

return pos
Output

Element found at index 4

You might also like