Solving notes (35) -- rotate the smallest element in the array

Source: Internet
Author: User

Problem description: Move the first several elements of an array to the end of the array, which is called the rotation of the array. Input a rotation of an sorted array and output the smallest element of the rotated array. For example, if an array {3, 4, 5, 1, 2} is a rotation of {1, 2, 3, 4, 5}, the minimum value of this array is 1. 

 
Idea: The most intuitive solution to this question is not difficult. Traverse the array from start to end to find the smallest element. The time complexity is obviously O (n ). However, this idea does not take advantage of the input array feature. Since there is an algorithm with lower time complexity, it is easy to think of binary search because its time complexity is O (logn ). Can this problem be solved by binary search? The answer is yes. Observe the features of the array, first increment (called increment A), then suddenly drop to the minimum value, and then increment (called increment B ). Of course, there is also a special case, that is, the number of elements to be rotated is 0.

In general, assume that A is the input array, left and right are the coordinates of the left and right boundary of the array, and evaluate the value of a [Mid] in the center. if a [Mid] <= A [right], it indicates that it is in incremental B, adjust the right boundary right = mid; if a [Mid]
> = A [left] indicates that it is in ascending a, so adjust left boundary left = mid. When the left and right boundary are adjacent, the smaller one is the minimum value of the array. In fact, in general, the elements in the right boundary refer to the minimum value.

In special cases, the number of rotations is 0. According to the above algorithm, the right boundary is reduced until it is adjacent to the left boundary. The element indicated by the left boundary is the minimum value. The following are several test cases:

// {,} 1 // {,} 1,} 1 // {,} 1 // {9, 9, 9, 9, 9, 9, 9, 10,} 9 Error

The result of the fifth group is incorrect. In fact, the above algorithms are suitable for strictly incrementing arrays. For non-strictly incrementing arrays, correct solutions cannot be ensured using a bipartite method. If you are interested, you can try to see if the correct solution can be obtained using a bipartite Method for a non-strictly incrementing sequence.

 
Reference code:

// Function: rotate the minimum element of the array // function parameter: parray points to the array, Len is the array length // return value: minimum element int findmin (int * parray, int Len) {If (parray = NULL | Len <= 0) return 0; int left = 0, Right = len-1, mid; while (right-left! = 1) {mid = left + (right-left)> 1); If (parray [right]> = parray [Mid]) Right = mid; else if (parray [left] <= parray [Mid]) Left = mid;} return parray [right]> parray [left]? Parray [left]: parray [right];}

 
I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985

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.