Interpolation search algorithm

definition of interpolation search

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 o…


This content originally appeared on DEV Community and was authored by Aya Bouchiha

definition of interpolation search

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.

Space complexity

The space complexity of interpolation search is O(1)

Time complexity of interpolation search

Best case Average case Worst case
O(1) O(log2(log2 n)) O(n)

Formula of interpolation search

the formula of interpolation search is:
pos = low + ((x – A[low]) * (high – low) // (A[high] – A[low]))
if you asked like me how we got this formula checkout this amazing article by Hitesh Kumar

Algorithm of interpolation search

  1. calculating the pos using the formula above
  2. if array[pos] == wantedElement: return pos
  3. if array[pos] > wantedElement : high = pos - 1
  4. if array[pos] < wantedElement : low = pos + 1
  5. stay repeating until the array[pos] == wantedElement or the sub-array reduces to zero.

Implementation of interpolation search using python

def InterpolationSearchAlgorithm(wantedItem: int, sortedItems: list):
    """
        => Algorithm Name: Interpolation search
        => Algorithm Type: search algorithms
        => Time Complexity: 
            [best case]     => O(1)
            [average case]  => O(log2(log2 n))
            [worst case]    => O(n)
        => Space Complexity => O(1)
        => Algorithm
            [1] =>  calculating the pos using the formula above
            [2] =>  if `array[pos] == wantedElement`: return pos
            [3] =>  if `array[pos] > wantedElement` : `high = pos - 1`
            [4] =>  if `array[pos] < wantedElement` : `low = pos + 1`
            [5] =>  stay repeating until the `array[pos] == wantedElement` or the sub-array reduces to zero.
        => params
            (wantedItem) => int
            (sortedItems) => sorted list and equally distributed.
        => Returns 
            (index) if the wanted item exists in the sortedItems
            (False) if the wanted item doesn't exist 
    """
    low = 0 
    high = len(sortedItems) - 1
    while (high >= low and sortedItems[high] >= wantedItem and sortedItems[low] <= wantedItem):
        # formula of interpolation sort
        pos = low + ((high - low) // (sortedItems[high] - sortedItems[low]) *
                    (wantedItem - sortedItems[low]))

        if sortedItems[pos] == wantedItem:
            # if it match return his index (pos)
            return pos
        elif sortedItems[pos] < wantedItem:
            # if A[pos] is smaller than wantedItem
            low = pos + 1
        else:
            # if A[pos] is larger than wantedItem
            high = pos - 1
    return False

References and useful resources

#day_7
Have a great day!


This content originally appeared on DEV Community and was authored by Aya Bouchiha


Print Share Comment Cite Upload Translate Updates
APA

Aya Bouchiha | Sciencx (2021-06-19T19:23:46+00:00) Interpolation search algorithm. Retrieved from https://www.scien.cx/2021/06/19/interpolation-search-algorithm/

MLA
" » Interpolation search algorithm." Aya Bouchiha | Sciencx - Saturday June 19, 2021, https://www.scien.cx/2021/06/19/interpolation-search-algorithm/
HARVARD
Aya Bouchiha | Sciencx Saturday June 19, 2021 » Interpolation search algorithm., viewed ,<https://www.scien.cx/2021/06/19/interpolation-search-algorithm/>
VANCOUVER
Aya Bouchiha | Sciencx - » Interpolation search algorithm. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/19/interpolation-search-algorithm/
CHICAGO
" » Interpolation search algorithm." Aya Bouchiha | Sciencx - Accessed . https://www.scien.cx/2021/06/19/interpolation-search-algorithm/
IEEE
" » Interpolation search algorithm." Aya Bouchiha | Sciencx [Online]. Available: https://www.scien.cx/2021/06/19/interpolation-search-algorithm/. [Accessed: ]
rf:citation
» Interpolation search algorithm | Aya Bouchiha | Sciencx | https://www.scien.cx/2021/06/19/interpolation-search-algorithm/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.