[Classic face question] Enter a rotation of an ordered array, outputting the smallest element of the rotated array.

Source: Internet
Author: User

"title"

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 an ordered 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.

"Analysis"

The most intuitive solution to this problem is not difficult. By iterating through the array one time, you can find the smallest element, and the time complexity is obviously O (N). But this idea does not take advantage of the characteristics of the input array, we should be able to find a better solution.

We notice that the rotated array can actually be divided into two sorted sub-arrays, and the elements of the preceding sub-arrays are greater than or equal to the elements of the post-face array. We can also notice that the smallest element is just the dividing line of these two sub-arrays. We are trying to find the smallest element with the idea of a two-dollar search method.

We get the elements in the middle of the array. If the intermediate element is in the preceding increment sub-array, it should be greater than or equal to the element that the first pointer points to. The smallest element in the array should be at the back of the intermediate element. We can point the first pointer to the middle element so that we can narrow the range we are looking for. Similarly, if an intermediate element is in a subsequent increment of a subarray, it should be less than or equal to the element that the second pointer points to. At this point the smallest element in the array should precede the intermediate element. We can point the second pointer to the middle element, which can also narrow the search. We then use the updated two pointers, to get and compare the new intermediate elements, loop down.

"Code"

/**********************************   Date: 2015-01-04*   sjf0115*   title: Enter a rotation of the sorted array, output the smallest element of the rotated array *   Blog: **********************************/#include <iostream>using namespace std;int searchmin (int a[],int n) {    if (n <= 0) {        return-1;    } If    int start = 0,end = N-1;    Array ordered    if (A[end] > A[start]) {        return a[start];    } If    //array rotation    //Two-point lookup    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<<searchmin (a,7) <<endl;    return 0;}


[Classic face question] Enter a rotation of an ordered array, outputting the smallest element of the 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.