public class Main {
public static void main(String[] args) {
int[] attr = { 1, 3, 4, 6, 7, 9 };
System.out.println(find(attr, 9));
System.out.println(find(attr, 0, attr.length, 9));
}
public static int find(int[] attr, int key) {
int s = 0;
int e = attr.length;
while (s <= e) {
int m = (s + e) / 2;
if (attr[m] == key) {
return m;
} else if (attr[m] < key) {
s = m + 1;
} else if (attr[m] > key) {
e = m - 1;
}
}
return -1;
}
public static int find(int[] attr, int s, int e, int key) {
int m = (s + e) / 2;
if (attr[m] == key) {
return m;
} else if (attr[m] < key) {
return find(attr, m + 1, e, key);
} else if (attr[m] > key) {
return find(attr, s, m - 1, key);
}
return -1;
}
}