Update Meta Binary Search.py

pull/343/head
Kavin18 2024-05-16 17:32:55 +05:30 zatwierdzone przez GitHub
rodzic 7b2e048bb7
commit 6b63ffea19
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 14 dodań i 23 usunięć

Wyświetl plik

@ -1,27 +1,18 @@
import math
def bsearch(A, key_to_search):
n = len(A)
lg = int(math.log2(n-1)) + 1;
pos = 0
for i in range(lg - 1, -1, -1) :
if (A[pos] == key_to_search):
return pos
new_pos = pos | (1 << i)
if ((new_pos < n) and
(A[new_pos] <= key_to_search)):
pos = new_pos
return (pos if(A[pos] == key_to_search) else -1)
n = len(A)
lg = int(math.log2(n-1)) + 1
pos = 0
for i in range(lg - 1, -1, -1):
if A[pos] == key_to_search:
return pos
new_pos = pos | (1 << i)
if new_pos < n and A[new_pos] <= key_to_search:
pos = new_pos
return pos if A[pos] == key_to_search else -1
if __name__ == "__main__":
A = list(map(int,input("Enter the array elements: ").split()))
key = int(input("enter the number to search: "))
print( bsearch(A, key))
A = list(map(int, input("Enter the array elements: ").split()))
key = int(input("Enter the number to search: "))
print(bsearch(A, key))