diff --git a/searches/interpolation_search_recursive.py b/searches/interpolation_search_recursive.py new file mode 100644 index 000000000000..bb1a4be4821f --- /dev/null +++ b/searches/interpolation_search_recursive.py @@ -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." + ) diff --git a/searches/meta_binary_search.py b/searches/meta_binary_search.py new file mode 100644 index 000000000000..94319aec3fa6 --- /dev/null +++ b/searches/meta_binary_search.py @@ -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." + )