1 public static int rank(int[] array, int k) 2 { 3 int front = 0, rear = array.length - 1; 4 5 while(front <= rear) //front = rear时, mid必与front和rear指向同一个元素 6 { 7 int mid = front + (rear - front) / 2; 8 if(k > array[mid]) front = mid + 1; 9 else if(k < array[mid]) rear = mid -1;10 else return mid;11 }12 return -1;13 }