[Classic Interview Questions] input a rotation of an sorted array to output the smallest element of the rotated array.

Source: Internet
Author: User

[Classic Interview Questions] input a rotation of an sorted array to output the smallest element of the rotated array.
[Question]

Moving the first several elements of an array to the end of an array is called the rotation of an 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.

[Analysis]

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. We should be able to find a better solution.

We noticed that the rotated array can actually be divided into two sort sub-arrays, and the elements of the sub-array are greater than or equal to the elements of the Back-face array. We can also note that the smallest element is the line between the two subarrays. We try to use the binary search method to find the smallest element.

We get the elements in the middle of the array. If the intermediate element is located in the first incremental sub-array, it should be greater than or equal to the element pointed to by the first pointer. At this time, the smallest element in the array should be behind the middle element. We can point the first pointer to this intermediate element to narrow down the search range. Similarly, if the intermediate element is located behind the incremental sub-array, it should be less than or equal to the element pointed to by the second pointer. At this time, the smallest element in the array should be located in front of the intermediate element. We can point the second pointer to the intermediate element, which can also narrow the search range. We then use the updated two pointers to get and compare the new intermediate elements and iterate.

[Code]

/********************************** Date: * Author: SJF0115 * Subject: enter a rotation of an sorted array and output the smallest element of the rotated array * blog: * *********************************/# include
 
  
Using namespace std; int SearchMin (int A [], int n) {if (n <= 0) {return-1 ;}// if int start = 0, end = n-1; // ordered array if (A [end]> A [start]) {return A [start];} // if // array rotation // binary search while (start <= end) {int mid = (start + end)/2; // [start, mid] ordered [mid, end] unordered if (A [mid]> A [start]) {start = mid;} // [start, mid] unordered [mid, end] ordered else if (A [mid] <A [start]) {end = mid;} else {return A [mid + 1] ;}} // while} int main () {int A [] = {2, 3, 4, 5, 6, 7, 8}; cout <
  
   

Related Article

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.