Minimum number of rotated array

Source: Internet
Author: User

"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

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.