Public class test {// known array a is shifted from the ordered array B (array B unknown) to the right n (0 <= n <= B. length) bit to get // such as a = {4, 5, 1, 2, 3} from B = {1, 2, 3, 4, 5} Move two places to the right to get the result. // search array A for the X Private Static int search (INT [] A, int H, int T, int X) element) {If (h> T) Return-1; int result =-1; int mid = (H + T)/2; if (a [Mid] = X) return mid; if (a [H] = A [T] & A [H] = A [Mid]) {// in this case, you cannot determine which side is increasing, there is a rotating array, as shown in the following // A = {5, X, 5, X, 5} // A = {5, 5, 5, 6, 5 }/ /A = {5, 6, 5, 5, 5} result = search (A, H, mid-1, x); If (result =-1) result = search (A, Mid + 1, t, x);} else if (a [H] <A [T]) {result = binarysearch (A, h, t, x);} else {// A [H]> A [T] | (A [H] = A [T] & A [H]! = A [Mid]) if (a [Mid]> = A [H]) {// A [H] ~ A [Mid] is incremental, and a [Mid + 1] ~ A [T] is the result of rotating the array = x> = A [H]? Binarysearch (A, H, mid-1, x): Search (A, Mid + 1, t, x);} else {// A [H] ~ A [Mid] is the rotating array, and a [Mid + 1] ~ A [T] is incremental result = x> = A [Mid + 1]? Binarysearch (A, Mid + 1, t, x): Search (A, H, mid-1, x) ;}} return result ;}// method 2, first find the number of digits of the array shift, and then determine which segment should be used for Binary Search Private Static int getmovestep (INT [] A, int H, int t) {If (h> = T) return 0; int low = H; int high = T; int mid = (low + high)/2; int step = 0; if (A [low] = A [High]) {// This is a situation where you cannot determine which side is an incremental sequence. Step = getmovestep (A, H, mid-1 ); if (step = 0) step = getmovestep (A, Mid + 1, t);} el Se {While (A [low]> A [High]) {mid = (low + high)/2; if (a [Mid]> = A [low]) {// increment sequence on the left of the MID step = low = Mid + 1;} else {// increment sequence on the right of the Mid high = mid-1 ;}} return step ;} // use y shift times to find Private Static int searchbymove (INT [] A, int X) {int result; int step = getmovestep (A, 0,. length-1); If (step! = 0) {result = x> A [0]? Binarysearch (A, 0, step-1, x): binarysearch (A, step,. length-1, x);} else {result = binarysearch (A, 0,. length-1, x);} return result;} Private Static int binarysearch (INT [] A, int H, int T, int X) {int low = h; int high = T; int mid; while (low <= high) {mid = (low + high)/2; if (a [Mid] = X) {return mid;} else if (a [Mid] <X) {LOW = Mid + 1;} else {high = mid-1 ;}} return-1 ;} public static void main (string [] ARGs) {int [] A = {4, 5, 1, 2, 3}; int [] B = {5, 5, 5, 7, 5}; int [] C = {5, 3, 5, 5}; int [] d = {5, 5, 3, 4, 5, 5, 5}; int [] e = {5, 5, 5, 5}; int [] f = {1, 2, 3, 4, 5}; system. out. println (search (A, 0,. length-1, 1); system. out. println (search (B, 0, B. length-1, 7); system. out. println (search (C, 0, C. length-1, 3); system. out. println (search (D, 0, D. length-1, 3); system. out. println (search (E, 0, E. length-1, 3); system. out. println (search (F, 0, F. length-1, 2); system. out. println (); system. out. println (searchbymove (A, 1); system. out. println (searchbymove (B, 7); system. out. println (searchbymove (C, 3); system. out. println (searchbymove (D, 3); system. out. println (searchbymove (E, 3); system. out. println (searchbymove (F, 2 ));}}