mirror of
https://github.com/c0de-archive/hacktoberfest-2018.git
synced 2024-12-22 05:12:40 +00:00
binary search in python
This commit is contained in:
parent
3f1d1da695
commit
2404975cd5
19
code/binary_search.py
Normal file
19
code/binary_search.py
Normal file
@ -0,0 +1,19 @@
|
||||
def binary(a, tv):
|
||||
minimum = 0
|
||||
maximum = len(a) - 1
|
||||
while minimum < maximum:
|
||||
guess = round((minimum + maximum)/2)
|
||||
if a[guess] == tv:
|
||||
return guess
|
||||
elif a[guess] < tv:
|
||||
minimum = guess + 1
|
||||
else:
|
||||
maximum = guess - 1
|
||||
return -1
|
||||
|
||||
|
||||
arr = []
|
||||
|
||||
ind = binary(arr, 4)
|
||||
print("index:")
|
||||
print(ind)
|
Loading…
Reference in New Issue
Block a user