From 2404975cd5e2194f577e79a2feacdd907838fa38 Mon Sep 17 00:00:00 2001 From: shutstart Date: Sat, 6 Oct 2018 10:54:26 +0530 Subject: [PATCH] binary search in python --- code/binary_search.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 code/binary_search.py diff --git a/code/binary_search.py b/code/binary_search.py new file mode 100644 index 0000000..ccdfd46 --- /dev/null +++ b/code/binary_search.py @@ -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)