Given a sorted array of n uniformly distributed values arr[], 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.

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 middle element to check. On the other hand interpolation search may go to different locations according the value of key being searched. For example if the value of key is closer to the last element, interpolation search is likely to start search toward the end side.

To find the position to be searched, it uses following formula.

// The idea of formula is to return higher value of pos

// when element to be searched is closer to arr[hi]. And

// 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[] [ad type=”banner”]

Algorithm
Rest of the Interpolation algorithm is 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 C implementation of algorithm.

[pastacode lang=”c” manual=”%2F%2F%20C%20program%20to%20implement%20interpolation%20search%0A%23include%3Cstdio.h%3E%0A%20%0A%2F%2F%20If%20x%20is%20present%20in%20arr%5B0..n-1%5D%2C%20then%20returns%0A%2F%2F%20index%20of%20it%2C%20else%20returns%20-1.%0Aint%20interpolationSearch(int%20arr%5B%5D%2C%20int%20n%2C%20int%20x)%0A%7B%0A%20%20%20%20%2F%2F%20Find%20indexes%20of%20two%20corners%0A%20%20%20%20int%20lo%20%3D%200%2C%20hi%20%3D%20(n%20-%201)%3B%0A%20%0A%20%20%20%20%2F%2F%20Since%20array%20is%20sorted%2C%20an%20element%20present%0A%20%20%20%20%2F%2F%20in%20array%20must%20be%20in%20range%20defined%20by%20corner%0A%20%20%20%20while%20(lo%20%3C%3D%20hi%20%26%26%20x%20%3E%3D%20arr%5Blo%5D%20%26%26%20x%20%3C%3D%20arr%5Bhi%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20Probing%20the%20position%20with%20keeping%0A%20%20%20%20%20%20%20%20%2F%2F%20uniform%20distribution%20in%20mind.%0A%20%20%20%20%20%20%20%20int%20pos%20%3D%20lo%20%2B%20(((double)(hi-lo)%20%2F%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20(arr%5Bhi%5D-arr%5Blo%5D))*(x%20-%20arr%5Blo%5D))%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Condition%20of%20target%20found%0A%20%20%20%20%20%20%20%20if%20(arr%5Bpos%5D%20%3D%3D%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20pos%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20larger%2C%20x%20is%20in%20upper%20part%0A%20%20%20%20%20%20%20%20if%20(arr%5Bpos%5D%20%3C%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20lo%20%3D%20pos%20%2B%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20smaller%2C%20x%20is%20in%20lower%20part%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20hi%20%3D%20pos%20-%201%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20-1%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20Code%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Array%20of%20items%20on%20which%20search%20will%0A%20%20%20%20%2F%2F%20be%20conducted.%0A%20%20%20%20int%20arr%5B%5D%20%3D%20%20%7B10%2C%2012%2C%2013%2C%2016%2C%2018%2C%2019%2C%2020%2C%2021%2C%2022%2C%2023%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2024%2C%2033%2C%2035%2C%2042%2C%2047%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%0A%20%20%20%20int%20x%20%3D%2018%3B%20%2F%2F%20Element%20to%20be%20searched%0A%20%20%20%20int%20index%20%3D%20interpolationSearch(arr%2C%20n%2C%20x)%3B%0A%20%0A%20%20%20%20%2F%2F%20If%20element%20was%20found%0A%20%20%20%20if%20(index%20!%3D%20-1)%0A%20%20%20%20%20%20%20%20printf(%22Element%20found%20at%20index%20%25d%22%2C%20index)%3B%0A%20%20%20%20else%0A%20%20%20%20%20%20%20%20printf(%22Element%20not%20found.%22)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]

Output :

Element found at index 4

Time Complexity : If elements are uniformly distributed, then O (log log n)). In worst case it can take upto O(n).
Auxiliary Space : O(1)

[ad type=”banner”]

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,