Python code to search a number in a sorted list

 To search a number in a sorted list

USING WHILE LOOP:

a=eval(input("Insert a list: "))
val=int(input("value to find:  "))
s=0
e=len(a)-1
while s<=e:
    m=(s+e)//2
    if val==a[m]:
        print(val,"found at",m)
        break
    elif a[m]<val:
        s=m+1
    else:
        e=m-1
else:
    print(val,"not found")

OUTPUT:



Comments

Popular posts from this blog

Sum of the digits present in a list

Python program to find hcf of two numbers

Python program to check a number is prime or not