Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added interpolation search #168

Merged
merged 1 commit into from
Oct 10, 2022

Conversation

webcoderspeed
Copy link
Contributor

Issue

  • Add Interpolation search #115

    Explanation of the algorithm

    • The interpolation search algorithm is an improvement over the binary search algorithm.
    • The interpolation 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.
    • 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]

    Pseudocode

      1. Find the position to be searched
      1. If it is a match, return the index of the item, and exit.
      1. 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.
      1. Repeat until a match is found or the sub-array reduces to zero.

    Example

    • interpolationSearch([1, 2, 3, 4, 5], 3) // returns 2
    • interpolationSearch([1, 2, 3, 4, 5], 6) // returns -1
    • interpolationSearch([1, 2, 3, 4, 5], 1) // returns 0

    Time Complexity

    • Best Case: O(1)
    • Average Case: O(log(log(n)))
    • Worst Case: O(n)

    Space Complexity

    • O(1)

@akshitagupta15june akshitagupta15june merged commit 7ba8ef9 into akshitagit:master Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants