public class BinarySearch {
/**
* @param args
*/
public static void main(String[] args) {
int[] int_arrays = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int value = 11;
int pos = BinarySearch(int_arrays, value);
System.out.println("the pos is:" + pos);
}
private static int BinarySearch(int[] int_arrays, int value) {
return rank(int_arrays, value, 0, int_arrays.length, 0);
}
private static int rank(int[] int_arrays, int value, int start, int length,
int depth) {
System.out.println("the depth:" + depth);
if (length <= 0) {
return -1;
}
int mid = start + (length - 1) / 2;
if (int_arrays[mid] > value) {
return rank(int_arrays, value, start, mid - start, depth + 1);
} else if (int_arrays[mid] < value) {
return rank(int_arrays, value, mid + 1, length - length / 2,
depth + 1);
} else {
return mid;
}
}
}
java二分查找
最新推荐文章于 2023-12-25 22:38:53 发布