Interview 8: Minimum number of rotating arrays (sword point offer)

Source: Internet
Author: User

Title: moving a number of elements at the beginning 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, and output the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of the array {1,2,3,4,5}, and the minimum value of the array is 1.

Topic Analysis:

We can find some of these rules by rotating the distribution of the elements in the subsequent array:

in the array {3,4,5,1,2}, the leftmost number (3) is greater than the rightmost number (2), because the original array is incremented, so the number on the left (3,4,5) is larger than the number on the right, so that we can divide the array into two sequences, namely sequence 1{3,4,5}, sequence 2{1,2}, we can find the minimum number 1 by binary lookup method.

Here's how to find it:

For example, the array is

int arr[5]={3,4,5,1,2};

Step one: Use two pointers to point to the first and last element of the array, i.e. arr[0]arr[4];

Step two: Find the middle element of the array arr[mid];

The third step: using the middle element and the first element of the array to compare with the first element and the last element, if the intermediate element is larger than the first element, then the intermediate element must be in the first sequence, then the first pointer to the middle element is arr[2]; If the middle element is small and the last element, Then the middle element must be in the second sequence, and the second pointer points to the middle element.

Fourth step: Repeat step two until the first pointer points to the last element of the first sequence, the second pointer to the first element of the second sequence, or two pointers adjacent, so the smallest element is the element to which the second pointer points.

650) this.width=650; "title=" Picture 1.png "src=" http://s5.51cto.com/wyfs02/M01/7C/D1/ Wkiol1bybdys6ypqaaaxpj8xcoq324.png "alt=" Wkiol1bybdys6ypqaaaxpj8xcoq324.png "/>

By analyzing the problem we can write the program very well, of course, we have to consider other cases, when the elements in the array are

{1,1,1,0,1} or {1,0,1,1,1}, we cannot tell if the intermediate element is in the first sequence, this is the smallest element we are going to find in another way.

The code is as follows:

#include <stdio.h> #include <stdlib.h> #include <assert.h>int min (int *arr,int  Size) {     assert (arr);      assert (size >=0);      int left = 0;     int right =  size - 1;     int mid = left;      while  (Arr[left] >= arr[right])      {           if  (right - left == 1)//If the two pointers are adjacent, the element to which the latter pointer refers is the minimum value        {           mid =  right;           break;       }      mid =  (left + right)  / 2;// Find the middle element       if  (arr[left] == arr[right)  &&  (arr[mid] == arr[left ])  //if the middle element and the left and right elements are equal, find the smallest element directly, and use the following function to implement the             return mininorder (arr,left,right);      if  (arr[mid] >=  arr[left])       {            left = mid;      }      else  if  (Arr[mid] <= arr[right])       {            right = mid;      }     }         return arr[mid];}  int mininorder (int *arr, int left, int right) {      int i = 0;     int min = arr[left];     for  (i =  left + 1; i < right; i++)//Go straight through the array to find the smallest element      {            if  (Arr[left]>arr[i])            {                min = arr[i];                break;          }      }     return min;}

Interview 8: Minimum number of rotating arrays (sword point offer)

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.