Professional Documents
Culture Documents
Binary Search: Source Code
Binary Search: Source Code
SOURCE CODE:
def binarysearch(List, elem):
beg = 0
last=len(List)-1
while(beg<=last):
mid=(beg+last)/2
if(elem==List[mid]):
return mid
elif(elem>List[mid]):
beg=mid+1
else:
last=mid-1
return -1
List=[0]*n
for j in range(n):
List[j]=input('Enter element:')
index=binarysearch(List, elem)
if index:
print'Element found at index', index, 'position =', index + 1
else:
OUTPUT: