"title" The smallest number of rotated arrays
Moves the first element of an array to the end of the array, which we call the rotation of the array. Enter a rotation of a non-descending sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of the array is 1. Note: All the given elements are greater than 0, and if the array size is 0, return null.
1 /**2 * Minimum number of rotation array3 * 4 * @authorAdministrator5 * 6 * Test case: null 1 1,2,3,4,5 3,4,5,1,2 1,0,1,1,1 1,1,1,0,1 2,3,4,5,1,2,2,27 * 8 * Complexity of Time: O (LOGN)9 */Ten Public classMain { One A Public Static voidMain (String args[]) { - - int[] Array = {2, 3, 4, 5, 1, 2, 2, 2 }; the -Main main =NewMain (); - -SYSTEM.OUT.PRINTLN ("min Num:" +main.getminnum (array)); + } - + PrivateInteger Getminnum (int[] Array) { A at if(NULL= = Array | | 0 = =array.length) { - return NULL; - } - - if(Array.Length = = 1) { - returnArray[0]; in } - to intHead = 0; + intTail = array.length-1; - intMiddle = 0; the * if(Array[head] <Array[tail]) { $ returnArray[head];Panax Notoginseng } - the while(Tail-head > 1) { + AMiddle = (head + tail)/2; the + //The value of the middle element is the same, cannot determine the direction of the pointer movement, to order to find - if(Array[head] = = Array[middle] && Array[middle] = =Array[tail]) { $ returnMinorder (array, head, tail); $ } - - //The middle element value is greater than the head element value, and the head pointer moves to the middle position the if(Array[head] <=Array[middle]) { -Head =Middle;Wuyi Continue; the } - Wu //Intermediate element value less than tail element value, trailing element moved to middle position - if(Array[tail] >=Array[middle]) { AboutTail =Middle; $ Continue; - } - } - A returnArray[tail]; + } the - PrivateInteger Minorder (int[] Array,intHeadinttail) { $ the intMinnum =Array[head]; the the for(inti = head; I <= tail; i++) { the if(Array[i] <minnum) { -Minnum =Array[i]; in } the } the About returnMinnum; the } the}
Minimum number of rotated array