P66, interview question 8: Minimum number of rotated arrays

Source: Internet
Author: User

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 {1,2,3,4,5}, and the minimum value of the array is 1.

Find the minimum value of the array in binary lookup, which is present in the transition from maximum to minimum, one pointer to the first element of the first ascending subarray, the second pointer to the last element of the second ascending subarray, and the element in the middle of the array, if it is larger than the element of the first pointer, Indicates that the middle pointer exists in the first ascending subsequence, pointing the first pointer to the element pointed to by the middle pointer, or, if the intermediate element is smaller than the element pointed to by the second pointer, indicates that the middle element exists in the second subsequence, and the second pointer points to the middle element. Test Case: 1) Functional Testing (the input array is a rotation of an ascending sorted array, with duplicate numbers or no duplicate numbers in the array). 2) Boundary value test (the input array is an array of ascending sort, containing only one array of numbers). 3) Special input test (input null pointer).

Implementation code:

 PackageCom.yyq;Importjava.util.Arrays;/*** Created by Administrator on 2015/9/10.*/ Public classFindmin { Public Static intFindmin (int[] Array) {        intLen = 0; inti = 0; intj = 0; intMID = 0; Try {            if(Array = =NULL|| Array.equals ("")) {                Throw NewException ("Invalid Parameters"); } Len=Array.Length; J= Len-1;  while(Array[i] >=Array[j]) {                if(j-i = = 1 | | j = =i) {Mid=J;  Break; } Mid= (i + j)/2; if(Array[i] = = Array[j] && Array[mid] = =Array[i])returnMininorder (ARRAY,I,J); if(Array[mid] >=Array[i]) I=mid; Else if(Array[mid] <=Array[j]) J=mid; }        }Catch(Exception e) {e.printstacktrace (); }        returnArray[mid]; }     Public Static intMininorder (int[] Array,intIintj) {        intresult = 0;  for(intm = i + 1; M <= J; m++){            if(Result >Array[m]) {Result=Array[m]; }        }        returnresult; }    //==================== test Code ====================     Public Static voidTest (int[] numbers,intexpected) {        intresult = 0; System.out.println ("The resource sequence is:");        System.out.println (arrays.tostring (numbers)); if(Numbers = =NULL|| Numbers.equals (""))return; Result=findmin (numbers); if(Result = =expected) System.out.println ("The minimum is" + result + ", passed!"); ElseSystem.out.println ("Fail!"); }     Public Static voidMain (string[] args) {//typical input, a rotation of an array of monotone ascending        intArray1[] = {3, 4, 5, 6, 0, 1, 2}; Test (Array1,0); //The smallest number that has a repeating number and the number that repeats exactly        intArray2[] = {3, 4, 5, 1, 1, 2}; Test (Array2,1); //There are duplicate numbers, but the number of repetitions is not the first number and the last number        intArray3[] = {3, 4, 5, 1, 2, 2}; Test (Array3,1); //There are duplicate numbers, and the number of repetitions is exactly the first number and the last number .        intArray4[] = {1, 0, 1, 1, 1}; Test (Array4,0); //monotone ascending array, rotated 0 elements, i.e. monotone ascending array itself        intArray5[] = {1, 2, 3, 4, 5}; Test (Array5,1); //There is only one number in the array        intArray6[] = {2}; Test (Array6,2); //Enter NULLTest (NULL, 0); }}
Output Result:The resource sequence is:[3, 4, 5, 6, 0, 1, 2]the minimum is 0, passed! The resource sequence is:[3, 4, 5, 1, 1, 2]the minimum is 1, passed! The resource sequence is:[3, 4, 5, 1, 2, 2]the minimum is 1, passed! The resource sequence is:[1, 0, 1, 1, 1]the minimum is 0, passed! The resource sequence is:[1, 2, 3, 4, 5]the minimum is 1, passed! The resource sequence is:[2]the minimum is 2, passed! The resource sequence Is:null

P66, interview question 8: Minimum number of rotated arrays

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.