Novice algorithm Learning path----dichotomy find Minimum in rotated Sorted Array

Source: Internet
Author: User

title: Suppose a rotated sorted array whose starting position is unknown (e.g. 0 1 2 4 5 6 7 May become 4 56 7 0 1 2).

You need to find the smallest of these elements.

You can assume that there are no duplicate elements in the array.

idea: first to exclude three extreme cases, empty, only one element, and the entire array is ordered.

When the sequence of the array is randomly rotated, it is divided into two order to be included in the topic of 4567 and 012, to find the middle number and the last element of the array comparison, if greater than the words to indicate the smallest number on the right side of the middle number, if less than the minimum number in the middle number of the left, and then continue to find by Note: The middle number does not appear equal to the last element, because there is no repeating element in the title.

question: I was thinking that the two arrays can be divided into two parts, two ascending array to find the minimum number, and finally proved wrong; Because the thinking is imprisoned in the dichotomy of the premise is a well-ordered array, the result is not thought to use the following method, this is my reference to other people's occasional, I simplified it again.

 Public intFindmin (int[] nums) {        //Write your code here         if(Nums = =NULL|| Nums.length = = 0)//first, three special cases.            return0; if(Nums.length = = 1)            returnNums[0]; intLow = 0; intHigh = Nums.length-1; intMID =Low ;  while(Low <High ) {Mid= (low + high)/2; if(Nums[mid] > Nums[high]) {//if the middle is greater than the last element that indicates the smallest number on the right of the middle numberLow = mid + 1;//the first half of the array is ruled out}Else if(Nums[mid] < Nums[high]) {//If the middle number is less than the last element, indicating the ascending part of the intermediate number, the most novel is on the left of the middle number.High =mid; }        }        returnNums[low];
}

Novice algorithm Learning path----dichotomy find Minimum in rotated Sorted 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.