/*** Function: Given a sorted array, containing n integers, but this array has been rotated several times, the number of times is unknown. Find an element in the array. * You can assume that the array elements were originally arranged in small to large order. */
/** * Idea: The array has been rotated, then look for inflection points. * @param A * @param left * @param right * @param x: element to search * @return */public static int search (int[] a,int left,int right, int x) {int mid= (left+right)/2;if (X==a[mid])//Find element return Mid;if (left>right) return-1;if (A[left]<a[mid]) {// The left half is the normal order if (X>=a[left]&&x<=a[mid]) {return search (a,left,mid-1,x);//Search the left half}else{return searches (A, mid+1 }}else if (A[mid]<a[right]) {//Right half is normal order if (X>=a[left]&&x<=a[mid]) {return Search (a , left,mid-1,x);//search for left half of}else{return searches (A, mid+1, right, x);//search for Half}}else if (A[left]==a[mid]) {//left half is repeating element if (A[mid] !=a[right]) {//if the right element is different, search for the right side of the return search (A, mid+1, right, x);//search for either side of}else{//otherwise search int result=search (A, left, mid=1, X if (result==-1) {return search (A, mid+1, right, x);} Elsereturn result;}} return-1;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
9.11 Sorting and Finding (iii)--given a sorted array containing n integers, but this array has been rotated multiple times to find an element in the array