Title: Move a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, outputting the smallest element of the rotated array. For example, the array {3, 4, 5, 1, 2} is a rotation of {l1,2,3, 4,5}, and the minimum value of the array is 1
The implementation code is as follows:
public class Test08 {/** * Moves a number of elements at the beginning of an array to the end of the array, which we call the rotation of the array. * Enter a rotation of an incrementally sorted array, outputting the smallest element of the rotated array. * For example, the array {3, 4, 5, 1, 2} is a rotation of {L, 2, 3, 4, 5}, the minimum value of the array is * * @param numbers rotation array * @return The minimum value of the array */public static int min (int[] numbers) {//Determine if input is valid if (numbers = = NULL | | numbers.length = = 0) {throw n EW runtimeexception ("Invalid input."); }//Start processing the first position int lo = 0; Start processing the last position int hi = numbers.length-1; Set the initial value int mi = lo; Make sure that the LO is in the previous ordered part, HI is in the latter part of the order while (Numbers[lo] >= Numbers[hi]) {//When the processing range has only two data, the latter result is returned Because Numbers[lo] >= Numbers[hi] is always true, the latter result corresponds to the smallest value if (Hi-lo = = 1) {return Numbers[hi]; }//Take the middle position mi = lo + (Hi-lo)/2; If the three numbers are equal, order processing is required to find the smallest value from beginning to End if (numbers[mi] = = Numbers[lo] && Numbers[hi] = Numbers[mi]) { RetuRN Mininorder (Numbers, lo, HI); }//If the corresponding value in the middle position is in the previous ordered part, set LO to the new processing position if (Numbers[mi] >= Numbers[lo]) {lo = mi; }//If the middle position corresponds to a value in the next well-ordered section, set hi to the new processing position else if (Numbers[mi] <= Numbers[hi]) { hi = mi; }}//Return the final processing result return NUMBERS[MI]; /** * Find the minimum value in the array * * @param numbers array * @param start of the start array * @param end of the array * @ret Urn found the smallest number */public static int Mininorder (int[] numbers, int start, int end) {int result = Numbers[start] ; for (int i = start + 1; I <= end; i++) {if (Result > Numbers[i]) {result = Numbers[i]; }} return result; } public static void Main (string[] args) {//typical input, one rotation of the monotonically Ascending array int[] Array1 = {3, 4, 5, 1, 2}; System.out.println (min (array1)); There are repeating numbers, and the number of repetitions is just the smallest number int[] Array2 = {3, 4, 5, 1, 1, 2}; System.out.println (min (array2)); There are repeating numbers, but the number of repetitions is not the first number and the last number int[] Array3 = {3, 4, 5, 1, 2, 2}; System.out.println (min (array3)); There are duplicate numbers, and the number of repetitions is exactly the first number and the last number int[] Array4 = {1, 0, 1, 1, 1}; System.out.println (min (array4)); Monotone ascending array, rotated 0 elements, i.e. monotonically Ascending array itself int[] Array5 = {1, 2, 3, 4, 5}; System.out.println (min (array5)); There is only one number in the array int[] Array6 = {2}; System.out.println (min (array6)); The numbers in the array are the same int[] Array7 = {1, 1, 1, 1, 1, 1, 1}; System.out.println (min (array7)); System.out.println (min (array6)); Enter null System.out.println (min (null)); }}
Operation Result:
"Sword refers to offer study" "Face question 8: Rotate the minimum number of arrays"