Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

suitabletitle

def search(a,k):
    for i in range(0,len(a)):
        if(a[i]==k):
            return 1
    return 0

def bs(a,k):
    mp=len(a)//2
    f=a[0:mp]
    s=a[mp:len(a)]
    
    if search(a,k)==0:
        return "false"
    else:
        if a[mp]==k:
            return mp
        else:
            if a[mp]>k:
                return bs(a[0:mp],k)
            else:
                if a[mp]<k:
                    return bs(a[mp:len(a)],k)+mp
    return bs(a,k)

a=[3,8,13,16,25,31,49,100]
k=31
print(bs(a,k))

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.