Create Interpolation Search.py

pull/343/head
Kavin18 2024-05-16 17:18:10 +05:30 zatwierdzone przez GitHub
rodzic 30e5b59bc0
commit f7cb26c461
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 32 dodań i 0 usunięć

Wyświetl plik

@ -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")