一个整数键和一个有序整数数组,返回键值在数组中的索引
int rank(int key, int[] a) { int low = 0; int high = a.length - 1; while (low <= high) { int mid = low + (high - low) / 2; if (key < a[mid]) { high = mid - 1; } else if (key > a[mid]) { low = mid + 1; } else { return mid; } } return -1; }