2016-12-06 39 views
-2
def binsearch(a,c): 

    first=0 
    last=len(a)-1 
    found=False 

    while first<=last and not found: 
     mid=(first+last)//2 
     if(a[mid]==c): 
      found=True   
     elif a[mid]<c: 
      last=mid-1 
     else: 
      first=mid+1 
    return found   
a=list() 

n=raw_input("Enter how many elements:") 

for i in range(int(n)): 

    num=raw_input("Enter the elements:") 
    a.append(int(num)) 


c=raw_input("Enter the element u wanna search:") 

b=binsearch(a,c) 

if b: 

    print "Element",c,"found in position." 

else: 

    print "Element not found." 

回答

1

錯誤是c是一個字符串。 只需在c = raw_input("...")行後加c = int(c)

+0

非常感謝您的幫助......它的工作原理 –

相關問題