Binary Search

 # Binary Search


def BinarySearch(L,ele):

bottom = 0

top = len(L)-1


while(True):

if (bottom > top):

print("Element not found")

return

   

mid = (bottom+top)//2

    

if(L[mid] == ele):

print("Element", ele, "present at position", mid+1)

return

elif (L[mid] < ele):

bottom = mid + 1

else:

top = mid - 1



L = eval(input("Enter list of elements : "))

L.sort()

ele = eval(input("Enter element to search : "))

BinarySearch(L,ele)


No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu