public class BinarySearch{ int[] a = {6,9,4,14,3,2,1,8,9,12,10,5,7}; void bubbleSort(int[] a){ if(a.length == 0){ System.out.println("错误!数组长度不能为0"); System.exit(1); } int temp = 0; for(int i=a.length-1;i>0;i--){ for(int j=0;j<i;j++){ if(a[j]>a[j+1]){ temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } } } void binarySearch(int a[],int i){ if(a.length == 0){ System.out.println("错误!数组长度不能为0"); System.exit(1); } bubbleSort(a); for(int j : a){ System.out.print(j+" "); } int m = 0; int start = 0; int end = a.length - 1; while(start <= end){ m = (start + end)/2; if(i == a[m]){ System.out.println(i + "排在第" + (m+1) + "个"); System.exit(1); }else if (i < a[m]){ end = m - 1; }else if (i > a[m]){ start = m + 1; } } System.out.println("没找到这个数!"); } public static void main(String[] args){ BinarySearch b = new BinarySearch(); b.binarySearch(b.a,10); } }