Outputs the smallest element in a rotated array that increments the sorted array--8

Source: Internet
Author: User

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 incrementally sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation for the array {1, 2, 3, 4, 5} with a minimum value of 1.

Of course, iterating through the array will definitely find the minimum, but its time complexity is O (N) and the efficiency is the lowest, so there should be a more efficient solution.

Since the original array is incremented, the first element of the array must be the minimum, and after the rotation, its minimum value becomes the point of rotation, so finding the minimum value of the array after a rotation can become a rotation point to find a rotating array. We can still find that the sequence before the point of rotation is ascending and orderly, and that the sequence after the rotation point is ascending and orderly, so it can be judged as follows:

    1. Take the middle value of the array;

    2. If the median value is larger than the leftmost value and is smaller than the right-most value, then this array is the increment array rotation amplitude is 0, the minimum value is the first value of the group, and returns directly;

    3. If the median value is smaller than the left one and is smaller than the right one, then the median is the rotation point, which is the minimum value, and returns directly;

    4. If the median value is larger than the leftmost value and is larger than the right-most value, then the middle value must be ascending and orderly to the left, while the rotation point must be in the middle value to the right, and the range will be indented to the median right half to start at the 1th step again;

    5. If the median value is smaller than the leftmost value and is smaller than the right-most value, then the median value to the right must be ascending and orderly, while the rotation point must be in the middle value to the left, and the range will be indented to the median left half to start at the 1th step;


The above analysis is actually equivalent to using a binary search to do, so that the time complexity is reduced to O (LgN), the following code to achieve:

#include  <iostream> #include  <assert.h>using namespace std;int find_min_ Num (const int *arr, size_t n) {    assert (arr && n);      int left = 0;    int right =  n-1;    int mid =  (Right-left)/2;    while (left  < right)     {           if ( (Arr[left] <= arr[mid])  &&  (Arr[mid] <= arr[right])          {             break;//when the array interval is incremented, the minimum value is the leftmost value         }         else if (Arr[mid-1] > arr[mid)  &&  (arr[mid+1] >  arr[mid]) &NBSP;&Nbsp;      {             return arr[mid];//when the median value is the rotation point, the minimum value is the median value         }         else if (Arr[left] <= arr[mid)  &&   (Arr[mid] >= arr[right])         {             left = mid + 1;// When the middle value is larger than the left and larger than the right         }         else        {             right = mid - 1;//Remove the above three cases there is only one, that is, the median value is smaller than the left line and smaller than the right          }        mid =  ( Right-left)/2&NBSP;+&NBSP;LEFT;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSp;      return arr[left];} Int main () {    int arr[] = {8, 9, 2, 3, 4, 5,  6, 7};        int ret = find_min_num (arr,  sizeof (arr)/sizeof (arr[0]);    cout<< "the min number is:  "&LT;&LT;RET&LT;&LT;ENDL;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;0;}


Run the program, the output of the result is 2;

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/AC/wKioL1com5CixJT5AAAM-xnU0SE147.png "title=" Min_ Num.png "alt=" Wkiol1com5cixjt5aaam-xnu0se147.png "/>

You can set the array to a different condition to test.



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1769837

Outputs the smallest element in a rotated array that increments the sorted array--8

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.