编辑代码

public class BinarySearch {  
    public static int binarySearch(int[] arr, int target) {  
        int left = 0;  
        int right = arr.length - 1;  
        while (left <= right) {  
            int mid = left + (right - left) / 2;  
            if (arr[mid] == target) {  
                return mid;  
            } else if (arr[mid] < target) {  
                left = mid + 1;  
            } else {  
                right = mid - 1;  
            }  
        }  
        return -1;  // 目标元素不存在于数组中  
    }  
  
    public static void main(String[] args) {  
        int[] arr = {85, 63, 52, 34, 25, 17, 6};  
        int target = 6;  
        int result = binarySearch(arr, target);  
        if (result != -1) {  
            System.out.println("目标元素存在于数组中,位置为:" + result);  
        } else {  
            System.out.println("目标元素不存在于数组中");  
        }  
    }  
}