How to program for binary search in python


How to program for binary search in python.


  1. def binary_search(search,lst):
     flag=0
     start=0
     end=len(lst)-1
     while start<=end :
     mid = (start + end) // 2
     if lst[mid] < search:
     start=mid+1
     elif lst[mid]>search:
     end=mid-1
     else:
     flag=1
     break
     if flag==0:
     print("not found.")
     else:
     print("Found.")
    elements=[]
    num_of_elmenets=int(input("how many elment you want to Enter: "))
    for i in range(num_of_elmenets):
     elm=int(input("Enter element "))
     elements.append(elm)
    elements.sort()
    ser=int(input("enter the value to search: "))

    binary_search(ser,elements)



Output:-


Post a Comment

0 Comments