Binary Search Program in Python

Python
Python


Code:

abc=[1,2,3,4,5,6,7,8,9,10]
n=len(abc)
loc=-1
mid=round((1+n)/2)
item=int(input("enter item "))
if(item==abc[mid]):
  print("data at Index" , mid)
  loc = mid
elif(item>abc[mid]):
    for i in range(mid+1,n,1):
      if(item==abc[i]):
        loc=i
        print("data at index ",loc)
else:    
    for i in range(mid-1,-1,-1):
      if(item == abc[i]):
        loc=i
        print("data at Index ",loc)
if(loc==-1):

    print("item not found")

Output:

enter item 8
data at index  7
>>> 

Tags: binary search,python,binary search algorithm,binary search in python,#binary search in python,binary search python,linear search,binary search example,binary search tree,algorithms in python,binary search algorithm in python,binary search in python hindi,how to code binary search in python,binary search algorithm iterative in python,linear search in python program,#binary search,linear search in python

Post a Comment

Previous Post Next Post