From 429154af2f63a39be346ca43fa5b3000690b51bc Mon Sep 17 00:00:00 2001 From: darshan totagi Date: Tue, 28 Oct 2025 12:32:55 +0530 Subject: [PATCH 1/2] feat: add Meta Binary Search algorithm in Python --- searches/interpolation_search_recursive.py | 17 +++++++++++++++++ searches/meta_binary_search.py | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 searches/interpolation_search_recursive.py create mode 100644 searches/meta_binary_search.py diff --git a/searches/interpolation_search_recursive.py b/searches/interpolation_search_recursive.py new file mode 100644 index 000000000000..dfb5c0f6d4c1 --- /dev/null +++ b/searches/interpolation_search_recursive.py @@ -0,0 +1,17 @@ +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..29ecfb4e38b4 --- /dev/null +++ b/searches/meta_binary_search.py @@ -0,0 +1,18 @@ +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.") From e16910be417cb973aafb616e055e0bf0ecaa69cb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Oct 2025 07:05:43 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/interpolation_search_recursive.py | 6 +++++- searches/meta_binary_search.py | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/searches/interpolation_search_recursive.py b/searches/interpolation_search_recursive.py index dfb5c0f6d4c1..bb1a4be4821f 100644 --- a/searches/interpolation_search_recursive.py +++ b/searches/interpolation_search_recursive.py @@ -14,4 +14,8 @@ def interpolation_search_recursive(arr, low, high, x): 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.") + 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 index 29ecfb4e38b4..94319aec3fa6 100644 --- a/searches/meta_binary_search.py +++ b/searches/meta_binary_search.py @@ -15,4 +15,8 @@ def meta_binary_search(arr, target): 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.") + print( + f"Element {target} found at index: {result}" + if result != -1 + else "Element not found." + )