diff --git a/Searching algorithms/Interpolation Search.py b/Searching algorithms/Interpolation Search.py new file mode 100644 index 0000000..f78c49f --- /dev/null +++ b/Searching algorithms/Interpolation Search.py @@ -0,0 +1,32 @@ +def interpolationSearch(arr, lo, hi, x): + + + if (lo <= hi and x >= arr[lo] and x <= arr[hi]): + + + pos = lo + ((hi - lo) // (arr[hi] - arr[lo]) * + (x - arr[lo])) + + if arr[pos] == x: + return pos + + if arr[pos] < x: + return interpolationSearch(arr, pos + 1, + hi, x) + + if arr[pos] > x: + return interpolationSearch(arr, lo, + pos - 1, x) + return -1 + +arr = list(map(int,input("Enter the array elements: ").split())) +n = len(arr) + +x = int(input("enter the number to search: ")) +index = interpolationSearch(arr, 0, n - 1, x) + +if index != -1: + print("Element found at index", index) +else: + print("Element not found") +