diff --git a/interpolation_iter.py b/interpolation_iter.py new file mode 100644 index 00000000..aeed302a --- /dev/null +++ b/interpolation_iter.py @@ -0,0 +1,50 @@ +# Python program to implement interpolation search +# using iteration approach +def interpolationSearch(arr, n, x): + + # Find indexes of two corners + low = 0 + high = (n - 1) + + # Since array is sorted, an element present + # in array must be in range defined by corner + while low <= high and x >= arr[low] and x <= arr[high]: + if low == high: + if arr[low] == x: + return low; + return -1; + + # Probing the position with keeping + # uniform distribution in mind. + pos = int(low + (((float(high - low)/( arr[high] - arr[low])) * (x - arr[low])))) + + # Condition of target found + if arr[pos] == x: + return pos + + # If x is larger, x is in upper part + if arr[pos] < x: + low = pos + 1; + + # If x is smaller, x is in lower part + else: + high = pos - 1; + + return -1 + +# Main function +if __name__ == "__main__": + # Array of items on whighch search will + # be conducted. + arr = [10, 12, 13, 16, 18, 19, 20, 21, + 22, 23, 24, 33, 35, 42, 47] + n = len(arr) + + x = 18 # Element to be searched + index = interpolationSearch(arr, n, x) + + # If element was found + if index != -1: + print ("Element found at index",index) + else: + print ("Element not found") diff --git a/interpolation_recurse.py b/interpolation_recurse.py new file mode 100644 index 00000000..3df84dec --- /dev/null +++ b/interpolation_recurse.py @@ -0,0 +1,51 @@ +# Python program to implement +# interpolation search with recursion +# If x is present in arr[0..n-1], then +# returns index of it, else returns -1. +# Time complexity is O(log(log(n)) in +# average case. O(n) in the worst case. + + +def interpolationSearch(arr, lo, hi, x): + + # Since array is sorted, an element present + # in array must be in range defined by corner + if (lo <= hi and x >= arr[lo] and x <= arr[hi]): + + # Probing the position while keeping + # uniform distribution in mind. + pos = lo + ((hi - lo) // (arr[hi] - arr[lo]) * + (x - arr[lo])) + + # Condition of target found + if arr[pos] == x: + return pos + + # If x is larger, x is in right subarray + if arr[pos] < x: + return interpolationSearch(arr, pos + 1,hi, x) + + # If x is smaller, x is in left subarray + if arr[pos] > x: + return interpolationSearch(arr, lo,pos - 1, x) + return -1 + +# Driver code + + +# Array of items in which +# search will be conducted +arr = [10, 12, 13, 16, 18, 19, 20, + 21, 22, 23, 24, 33, 35, 42, 47] +n = len(arr) + +# Element to be searched +x = 18 +index = interpolationSearch(arr, 0, n - 1, x) + +if index != -1: + print("Element found at index", index) +else: + print("Element not found") + +