Code:
n=int(input("Enter the size of ur list: "))
lis1=[]
for i in range(n):
ele=int(input(f"Enter element {i+1}: "))
lis1.append(ele)
print(f"Your list is: \n{lis1}")
key=int(input("Enter the element you wish to find: "))
def sort_list(lis):
sorted_list=[]
while lis:
small_num=min(lis)
sorted_list.append(small_num)
lis.remove(small_num)
print(sorted_list)
return sorted_list
sorted_list=sort_list(lis1)
def binary_search(upper,lower=0):
result=None
while lower<=upper:
mid=(lower+upper)//2
if sorted_list[mid]==key:
result=mid
elif sorted_list[mid]<key:
binary_search(upper,mid+1)
else:
binary_search(mid-1)
return result
if binary_search(n-1)==None:
print("Your no is not present in the list!")
else:
print(f"{key} was found at position {result}!")