4259 links
  • Arnaud's links
  • Home
  • RSS Feed
  • Tag cloud
  • Login
search: tag:nomarkdown tag:search — newest first
  • BInary search algo

    def binary_search(l, item):
    first = 0
    last = len(l)-1
    found = False

    while first<=last and not found:
        midpoint = round((first + last)/2)
        if l[midpoint] == item:
            found = True
        else:
            if item < l[midpoint]:
                last = midpoint-1
            else:
                first = midpoint+1
    
    return found
    

    input = [0, 1, 2, 8, 13, 17, 19, 32, 42,]

    print(binary_search(input, 3)) # found: False
    print(binary_search(input, 13)) # fount: True

    February 6, 2022 - permalink - https://links.infomee.fr/shaare/GnzD5Q
    algo nomarkdown search