package BinarySearch; import java.lang.reflect.Array; public class Test { public static void main(String[] args) { int arr[] = {2,6,7,8,9,11,13,45,123,544,600 }; int result = search(arr, 45, 0, arr.length-1 ); System.out.println(result); } //二分查询 public static int search (int arr[], int key, int min, int max) { if(min>max) return -1; while (true) { int mid = (min + max) / 2; if (arr[mid] > key) { max = mid - 1; } else if (arr[mid] < key) { min = mid + 1; } else { return mid; } } } }
标签:二分,arr,封装,min,int,max,mid,java From: https://www.cnblogs.com/p201821460026/p/16732419.html