+ 2

What is the wrong in this code

def binarysearch(a,key): start=0;end=n-1 while (start<=end): mid=(start+end)//2 if (a[mid]==key): return (key,"found at",i,"position") elif (key>a[mid]): start=mid+1 else: end=mid-1 return("element not found") a=[] n=int(input("enter list size")) for i in range(1,n+1): a.append (input("enter",i,"element")) print (a) a.sort() print ("after sorting",a) key=input("enter search element") binarysearch (a,key)

26th Mar 2020, 2:55 PM
Abhinav Racharla
Abhinav Racharla - avatar
3 Answers
+ 2
You need to do the following: 1. last line must be print(binarysearch(a,key)) 2. end = n-1 must be end = len(a) -1 3. key, ‘found at’, i must be key, ‘found at’, mid + 1
26th Mar 2020, 3:22 PM
Gabriel Ilie
Gabriel Ilie - avatar
+ 1
Change input("enter", i, "element") to input("enter" + str(i) + "element")
26th Mar 2020, 3:25 PM
Russ
Russ - avatar
0
OK bro tq..
28th Mar 2020, 6:26 AM
Abhinav Racharla
Abhinav Racharla - avatar