Rotate the minimum value of an array

Source: Internet
Author: User

Problem

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 ordered 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.

Ideas

The most intuitive solution to this problem is not difficult. By iterating through the array one time, you can find the smallest element, and the time complexity is obviously O (n). But this idea does not take advantage of the input array's characteristics. Since there is a smaller algorithm for time complexity, it is easy to think of binary lookups because of its time complexity O (logn). Can this question be used to find the two points? The answer is yes. Observe the properties of the array, increment it first (called Increment a), and then suddenly descend to the minimum and then increment (called Increment b). Of course, there is a special case, that is, the array increment, the middle does not fall, that is, the number of rotation elements is 0.

General solution: To find the middle subscript, if the array start value is less than the middle subscript corresponding to the value, then the middle subscript is the first increment, the subscript is equal to the middle subscript, in contrast, if the middle subscript corresponding to the value is less than the end value, then the middle subscript input second increment, the other end subscript equals the middle subscript. The subscript and end subscript are adjacent until the beginning.

Special case: The number of rotations is 0, for example {1,2,3,4,5}, you can directly output the first value by the first value less than the last value.

 Packageoffer008;ImportJava.util.Scanner;/*** @Title: Main.java * @Package: offer008 * @Description The smallest number of rotated arrays *@authorHan * @date 2016-4-18 a.m. 10:39:35 *@versionV1.0*/        Public classMain { Public Static voidMain (string[] args) {Scanner Scanner=NewScanner (system.in); int[] arr =NULL; intCount = 0;  while(Scanner.hasnext ()) {//size of the input arrayCount =Scanner.nextint (); Arr=New int[Count]; //Input Array             for(inti = 0; I < count; i++) {Arr[i]=Scanner.nextint ();        } System.out.println (arr); }    }             /*** @Description Get the minimum value in the rotated array *@authorHan *@paramarr *@returnmin*/              Private Static intMinint[] arr) {                //determines whether the array is empty and whether the length is 0        if(arr = =NULL|| Arr.length = = 0){                        Throw NewRuntimeException ("Error Input"); }                //Head Subscript        intStartIndex = 0; //Tail Subscript        intEndIndex = arr.length-1; //Intermediate Subscript        intMiddleindex = 0; //if the first value in the array is less than the last value, it indicates that the array is an incrementing array, and the first element is directly output         while(Arr[startindex] >=Arr[endindex]) {            //if the startindex and Endindex are adjacent, the number that Endindex points to is the minimum value            if(Endindex-startindex = = 1) {StartIndex=EndIndex;  Break; } Middleindex= (StartIndex + endIndex)/2; //if three values are equal, similar to 1 0 1 1 1 1, you need to find the minimum value in order            if(Arr[middleindex] = = Arr[startindex] && Arr[middleindex] = =Arr[endindex]) {                                returnMinbyorder (arr, StartIndex, EndIndex); }                        //If the median value is greater than the value pointed to by startindex, move the startindex to the middle value            if(Arr[middleindex] >=Arr[startindex]) {StartIndex=Middleindex; }Else if(Arr[middleindex] <=Arr[endindex]) {EndIndex=Middleindex; }        }                returnArr[startindex]; }         /*** Find the minimum value @Description order *@authorHan *@paramarr *@paramStartIndex *@paramEndIndex *@return       */              Private Static intMinbyorder (int[] arr,intStartIndex,intEndIndex) {        intMin =Arr[startindex];  for(inti = StartIndex + 1; I <= EndIndex; i++){                        if(Arr[i] <min) {min=Arr[i]; }        }                returnmin; }}
Results

  

Rotate the minimum value of an array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.