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

Execute Python-3 Online

#Kenny Centeno
#Y00409919
#COMP 2900
#Seccion: 72584

def binarySearch(alist, item):
    #defining the class an arraylist
    first = 0 
    #assigns 0 to the first element of the arraylist
    last = len(alist)-1
    #sets the last element of the arraylist
    found = False
    #the value entered is false
    
    while first <= last and not found:
        midpoint = (first + last)//2
        #while the value entered is less or equal to 0 and isn't false the next this instruction will be applied
        if alist[midpoint] == item:
            found = True 
        else:
            if item < alist[midpoint]:
                last = midpoint-1
            else: 
                first = midpoint+1
    
    return found
    #if the element isn't found in the arraylist then the program returns found
    
testlist = [0 , 1, 2, 8, 13 , 17, 19, 32, 42,]
#list with all the elements values assign
print(binarySearch(testlist, 3))
#prints false because 3 isn't in one of the values
print(binarySearch(testlist, 13))
#prints true because 13 is a value present in the lis

#code from Problem Solving with Algorithms and Data Structures using Python

Advertisements
Loading...

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