编辑代码

public class BinarySearch {

    // 二分查找算法
    public static boolean binarySearch(int[] array, int target) {
        int left = 0;
        int right = array.length - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;

            if (array[mid] == target) {
                return true; // 元素找到
            } else if (array[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        return false; // 元素未找到
    }

    public static void main(String[] args) {
        int[] sortedArray = {85, 63, 52, 34, 25, 17, 6};
        int targetElement = 6;

        boolean isElementFound = binarySearch(sortedArray, targetElement);

        if (isElementFound) {
            System.out.println("元素 " + targetElement + " 存在于数组中。");
        } else {
            System.out.println("元素 " + targetElement + " 不存在于数组中。");
        }
    }
}