Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions searches/interpolation_search_recursive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def interpolation_search_recursive(arr, low, high, x):
"""Recursive implementation of Interpolation Search."""
if low <= high and arr[low] <= x <= arr[high]:
pos = low + ((high - low) * (x - arr[low])) // (arr[high] - arr[low])
if arr[pos] == x:
return pos
if arr[pos] < x:
return interpolation_search_recursive(arr, pos + 1, high, x)
return interpolation_search_recursive(arr, low, pos - 1, x)
return -1


if __name__ == "__main__":
arr = [10, 20, 30, 40, 50, 60, 70]
x = 50
result = interpolation_search_recursive(arr, 0, len(arr) - 1, x)
print(
f"Element {x} found at index: {result}"
if result != -1
else "Element not found."
)
22 changes: 22 additions & 0 deletions searches/meta_binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def meta_binary_search(arr, target):
"""Meta Binary Search using bit manipulation."""
n = len(arr)
pos = -1
for i in reversed(range(n.bit_length())):
new_pos = pos + (1 << i)
if new_pos < n and arr[new_pos] <= target:
pos = new_pos
if pos != -1 and arr[pos] == target:
return pos
return -1


if __name__ == "__main__":
arr = [2, 5, 7, 9, 14, 18, 21, 25]
target = 18
result = meta_binary_search(arr, target)
print(
f"Element {target} found at index: {result}"
if result != -1
else "Element not found."
)