在循环升序数组中查找一个数,返回这个数在数组中的index,否则返回-1。 递归算法 public static int SearchInRotateArray_Recursion(int[] ra, int low, int high, int value) { int mid = (low + high) / 2; if (low > high) return -1; if (ra[mid] == value) return mid; if (ra[low] <= ra[mid]) { //1. Left side is sorted if (ra[low] <= value && value <= ra[mid]) { //1.1 The value drops in the left half return SearchInRotateArray_Recursion(ra, low, mid - 1, value); } else { //1.2 The value drops in the right half return SearchInRotateArray_Recursion(ra, mid + 1, high, value); } } else { //2. Right side is sorted if (ra[mid] <= value && value <= ra[high]) { //2.1 The value drops in the right half return SearchInRotateArray_Recursion(ra, mid + 1, high, value); } else { //2.2 The value drops in the left half return SearchInRotateArray_Recursion(ra, low, mid - 1, value); } } } 循环算法 public static int SearchInRotateArray_Loop(int[] ra, int low, int high, int value) { while (low <= high) { int mid = (low + high) / 2; if (ra[mid] == value) return mid; if (ra[low] <= ra[mid]) { //1. Left side is sorted if (ra[low] <= value && value <= ra[mid]) { //1.1 The value drops in the left half high = mid - 1; } else { //1.2 The value drops in the right half low = mid + 1; } } else { //2. Right side is sorted if (ra[mid] <= value && value <= ra[high]) { //2.1 The value drops in the right half low = mid + 1; } else { //2.2 The value drops in the left half high = mid - 1; } } } return -1; }