One minute to learn recursive binary search - python implementation

def binarySearchRecursion(ls, target, left, right):
    if(target < ls[left]) or (target > ls[right]):
        return "没有找到"
    else:

        #python divisor // can be understood as indicating rounding down. For example: 7//2 = 3
        mid = (left + right)//2

        #If the three recursive branches are equal, return directly; if greater than...; if less than...
        if (target == ls[mid]):
            return mid
        elif target > ls[mid]:
            return binarySearchRecursion(ls, target, mid+1, right)
        else:
            return binarySearchRecursion(ls, target, left, mid-1)
        

Guess you like

Origin blog.csdn.net/qq_50709355/article/details/123315458