1 public class twosep { 2 3 public static void main(String[] args) { 4 twosep ts = new twosep(); 5 int arr[] = {1,2,3,6,8,9,11,23,28,56,59,61,62,63,66,68,70,78,79,91,92}; 6 ts.nofind(1, arr); 7 8 } 9 public void find(int num,int[] arr,int min,int max){ 10 int mid = (min + max)/2; 11 if(num==arr[mid]){ 12 System.out.println("你查找的位置为"+mid); 13 }else if(num>arr[mid]){ 14 min = mid + 1; 15 find(num,arr,min,max); 16 }else{ 17 max = mid - 1; 18 find(num,arr,min,max); 19 } 20 } 21 public void nofind(int num,int[] arr){ 22 int low=0; 23 int high = arr.length; 24 while(low<=high){ 25 int mid = (low+high)/2; 26 if(num==arr[mid]){ 27 System.out.println("你查找的值位置为"+mid); 28 break; 29 }else if(num>arr[mid]){ 30 low = mid+1; 31 }else{ 32 high = mid -1; 33 } 34 } 35 } 36 }