编辑代码

def binary_search(arr, target):
    left, right = 0, len(arr) - 1

    while left <= right:
        mid = (left + right) // 2

        if arr[mid] == target:
            return True  # 元素找到
        elif arr[mid] < target:
            left = mid + 1  # 在右半部分继续搜索
        else:
            right = mid - 1  # 在左半部分继续搜索

    return False  # 元素不在数组中

def main():
    # 有序数组
    sorted_array = [6, 17, 25, 34, 52, 63, 85]

    # 要查找的元素
    target_element = 6

    # 在有序数组中查找元素
    result = binary_search(sorted_array, target_element)

    # 输出结果
    if result:
        print(f"元素 {target_element} 存在于数组中。")
    else:
        print(f"元素 {target_element} 不存在于数组中。")

if __name__ == "__main__":
    main()