Skip to content

Commit 8790932

Browse files
Create meta-binary-search.py
Added a new function meta_binary_search that efficiently locates elements in a sorted array using a modified binary search algorithm. This approach constructs the index of the target element incrementally, potentially reducing the number of comparisons required. The function returns the index of the target element if found, or -1 otherwise.
1 parent 03a4251 commit 8790932

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

searches/meta-binary-search.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def meta_binary_search(arr, target):
2+
low = 0
3+
high = len(arr) - 1
4+
while low <= high:
5+
mid = (low + high) // 2
6+
if arr[mid] == target:
7+
return mid
8+
elif arr[mid] < target:
9+
low = mid + 1
10+
else:
11+
high = mid - 1
12+
return -1
13+
14+
def main():
15+
arr = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
16+
target = 23
17+
result = meta_binary_search(arr, target)
18+
if result != -1:
19+
print("Element found at index: ", result)
20+
else:
21+
print("Element not found!")
22+
23+
if __name__ == "main":
24+
main()

0 commit comments

Comments
 (0)