public static int ergodicFind(int[] arr,int key) { //折半查找
int min = 0;
int max = arr.length-1;
int mid = (max+min)/2;
while (arr[mid]!=key) {
if (key<arr[mid]) {
max = mid-1;
}else if (key>arr[mid]) {
min = mid+1;
}
mid = (max+min)/2;
}
return mid;
}作者: 画问好送明天 时间: 2015-8-26 22:55
class ArrayDemo4 { public static void main(String[] args) { int[] arr = {18,16,14,25,8,11}; int index = getIndex(arr,8); System.out.println("index = "+index); } public static int getIndex(int[] arr,int key) { for (int x = 0;x <arr.length ;x++ ) { if (arr[x] == key) return x; } return -1; } }作者: 张兵 时间: 2015-8-26 23:55