def binary_search(array, x):
    """
    Binary search
    :param array: array: Must be sorted
    :param x: value to search
    :return: position where it is found, -1 if not found
    """
    lower = 0
    upper = len(array)
    while lower < upper:   # use < instead of <=
        mid = lower + (upper - lower) // 2  # // is the integer division
        val = array[mid]
        if x == val:
            return mid
        elif x > val:
            if lower == mid:
                break
            lower = mid
        elif x < val:
            upper = mid

    return -1