BINARY SEARCH IN PYTHON
Binary Search Algorithm
Program :
list1 = [10,20,30,40,50,60,70,80,90,100]
search_element = int(input('Enter the element to search : '))
start = 0
end = len(list1)-1
while end >= start:
mid = (start+end)//2
if list1[mid] == search_element:
print('The element %d is present in the list at positon %d'%(search_element,mid+1))
break
elif list1[mid] > search_element:
end = mid-1
elif list1[mid] < search_element:
start = mid+1
else :
print('The element %d is not present in the list'%search_element)
Output :
Enter the element to search : 100
The element 100 is present in the list at positon 10
Also Refer : Linear Search In Python
Comments