Interpolation Search
Interpolation search is an algorithm for searching for a target value in a sorted array. It's similar to binary search in that it repeatedly divides the search interval in half, but it uses the information about the relative position of the target value to the values at the indices of the array.
Example in TypeScript
This function takes two arguments: an array and a target value. It uses a while loop to repeatedly divide the search interval in half. Inside while loop, it first calculate the position of the element that we are looking for by pos = low + ((target - arr[low]) * (high - low)) / (arr[high] - arr[low])
This formula uses the information about the relative position of the target value to the values at the indices of the array. Then, it check if the element at that position is target. If it is, it returns the index. If it's less than the target, it updates the low
variable to be one greater than the current position. If it's greater than the target, it updates the high
variable to be one less than the current position. if the target is not in the array, this function returns null
.
Usage
The interpolation search is generally more efficient than binary search when the values in the array are uniformly distributed, but it can be less efficient when the values are not uniformly distributed because it can lead to a large number of iterations.
Last updated