package chapterone;
public class Exercise1_1_22 {
public static int rank(int key, int[] array) {
return rank(key, array, 0, array.length - 1, 0);
}
public static int rank(int key, int[] array, int low, int high, int indent) {
if (low > high) {
return -1;
}
int mid = (low + high) / 2;
System.out.println(repeat(4 * indent, ' ') + low + high);
if (key > array[mid]) {
return rank(key, array, mid + 1, high, ++indent);
} else if (key < array[mid]) {
return rank(key, array, low, mid - 1, ++indent);
} else {
return mid;
}
}
public static String repeat(int n, char c) {
String s = "";
for (int i = 0; i < n; i++) {
s += c;
}
return s;
}
public static void main(String[] args) {
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
System.out.println(rank(3, array));
}
}
Exercise1.1.22
最新推荐文章于 2021-06-19 17:54:05 发布