I am a python beginner and I wrote a program that compares between the binary search method for finding a number and the linear one. However, when I run it It does not function properly and would stop when I enter an input and print nothing. Please, may someone explain what is wrong with it? THANK YOU SO MUCH
import random
import time
n=1000000
start = time.time()
lst=[random.randint(0,100*n) for i in range(n)]
start=time.time()
def linear():
for c in lst:
if c==e:
return "found"
else:
return "not found"
end = time.time() # record end time
print('time to sort:', (end-start)/100, 'seconds')
print(linear())
lst=[random.randint(0,100*n) for i in range(n)]
start=time.time()
def binary(e,lst):
Ubound=len(lst)-1
Lbound=0
mid=(Ubound+Lbound)//2
found=True
lst.sort()
while found and Lbound<=Ubound:
if e==lst[mid] :
found=True
return found
elif lst[mid]<e:
Lbound=mid+1
else:
Ubound=mid-1
return found
end=time.time()
print('time to sort:', (end-start)/100, 'seconds')
e=int(input("what are you looking for?"))
if binary(e,lst):`enter code here`
print("item has been found")
else:
print("item has not been found")