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 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 {1,2,3,4,5}, and the minimum value of the array is 1
This problem can be found with two points of thought:
The time complexity of binary lookup is: O (LOGN)
/** * 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, * output 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 * time: August 27, 2015 10:40:25 * file: Min.java *cutter_point */package Bishi. Offer50.y2015.m08.d27;public class min{/** * Use dichotomy to find the smallest value in a rotated array * @param numbers * @return * @throws Exception */public Stati c int getmin (int numbers[]) throws exception{if (numbers = null) {throw new Exception ("Array is empty");} Ifint Minnumber = 0;int start = 0;int end = Numbers.length-1;int mid = Start;while (Start < end) {if ((end-start) = = 1) {minnumber = Numbers[end];break;} Ifmid = (start + end)/2;//takes into account when start and end have the same value as the MID3 place//In order to find if (numbers[start] = = Numbers[mid] && numbers[ MID] = = Numbers[end]) {return Minorder (numbers, start, end);} IfIf (Numbers[mid] > Numbers[start]) {start = mid;} Ifelse{end = mid;} Else}//whilereturn Minnumber;} public static int Minorder (int numbers[], int index1, int index2) {int result = Numbers[index1];while (index1 <= index2) { if (Result > numbers[index1]) result = Numbers[index1];++index1;} Whilereturn result;} public static void Main (string[] args) {int array[] = {3,4,5,0,1,1,2,3};int array2[] = {1,0,1,1,1};try{system.out.print ( Getmin (array));} catch (Exception e) {e.printstacktrace ();}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Written test" 26, minimum number of rotated array