2016-07-23 20:03:51 +08:00
|
|
|
def binarySearch(alist, item):
|
|
|
|
|
|
|
|
first = 0
|
|
|
|
last = len(alist)-1
|
|
|
|
found = False
|
|
|
|
|
|
|
|
while first<=last and not found:
|
|
|
|
|
|
|
|
midpoint = (first + last)//2
|
|
|
|
if alist[midpoint] == item:
|
|
|
|
found = True
|
2016-07-27 00:53:22 +08:00
|
|
|
print("Found [ at position: %s ]" % (alist.index(item) + 1))
|
2016-07-23 20:03:51 +08:00
|
|
|
else:
|
|
|
|
|
|
|
|
if item < alist[midpoint]:
|
|
|
|
|
|
|
|
last = midpoint-1
|
|
|
|
else:
|
|
|
|
first = midpoint+1
|
2016-07-27 00:53:22 +08:00
|
|
|
|
2016-07-23 20:03:51 +08:00
|
|
|
if found == False:
|
2016-07-27 00:53:22 +08:00
|
|
|
continue
|
|
|
|
# print("Not found")
|
2016-07-23 20:03:51 +08:00
|
|
|
return found
|
|
|
|
print("Enter numbers seprated by space")
|
|
|
|
s = input()
|
|
|
|
numbers = list(map(int, s.split()))
|
|
|
|
trgt =int( input('enter a single number to be found in the list '))
|
|
|
|
binarySearch(numbers, trgt)
|
|
|
|
|