Rotate an array to find the smallest element

Source: Internet
Author: User

The title describes 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 a non-descending sorted 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.
Note: all elements given are greater than 0, and if the array size is 0, return 0.If you want to find a number in a sorted array or count the number of occurrences of a number, you can try a binary lookup algorithmanalysis:

The rotated array can actually be divided into two ordered sub-arrays: the size of the preceding sub-array is greater than the elements in the Back-face array

Notice that the smallest element is actually the dividing line of two sub-arrays . The array given in this topic is sorted to some extent, so we try to find the smallest element with a binary search method .

Ideas:

(1) we use two pointers start,end to the first and last element of the array, respectively. According to the rules of rotation of the topic, the first element should be greater than the last element (no duplicate elements).

But if it's not a rotation, the first element is definitely smaller than the last Element.

(2) Locate the middle element of the Array.

When the middle element is greater than the first element , the intermediate element is in the preceding incrementing Subarray , at which point the smallest element is behind the middle element . We can have the first pointer start pointing to the middle Element.

After the move, the first pointer is still in the preceding increment array.

When the intermediate element is less than the first element , the intermediate element is in the following incrementing Subarray , at which point the smallest element is in front of the middle element . We can let the second pointer end point to the middle Element.

After the move, the second pointer is still in the subsequent incrementing array.

This narrows the scope of the Search.

(3) according to the above idea, the first pointer start always points to the element that increments the array earlier, and the second pointer end always points to the array element that is incremented Later.

finally, the first pointer points to the last element in the preceding array, and the second pointer points to the first element in the subsequent array.

That means they point to two adjacent elements, and the second pointer points to the smallest element , which is the end condition of the loop .

So far the above ideas have solved the situation without repeating numbers , this topic added to this request, with repeated Numbers.

therefore, This topic has more special cases than the previous topic:

Let's look at a set of Examples: {1,0,1,1,1} and {1, 1, 1,0,1} can all be seen as a rotation that increments the sorted array {0,1,1,1,1}.

In this case we can not continue to use the solution of a problem to solve the Problem. Because in both arrays, the first number, the last number, and the median number are 1.

In the first case, the median number is in the following sub-array, and the second case, The median number is in the preceding sub-array.

So when the two pointers point to the same number as the middle number, we cannot determine if the median number 1 is the preceding sub-array (green Representation) or the subsequent sub-array (purple representation).

You cannot move the pointer to narrow the Search.

Import Java.util.arraylist;public class solution {public int minnumberinrotatearray (int [] array) {if (array.leng      Th==0) {return 0; }//set two pointers, start,end int start=0;//start point to No. 0 element int End=array.length-1;//end point to the last element if (array[            Start]<array[end]) {return Array[start];//because of this array, each sub-array inside is incremented} while (start!=end-1) {            int mid= (start+end)/2; If (array[mid]>=array[start]) {start=mid;//points the first pointer to an intermediate element} if (array[mid]<=array[e Nd]) {end=mid;//points The last pointer to the middle element} if (array[start]==array[end]&&array[mid]==arra            Y[start]) {return Minorder (array,start,end);    }} return array[end];        }//gets the minimum value private static int minorder (int[] array,int start,int end) {int result=array[start]; For (int i=start+1;i<=end;i++) {if (result>array[i]) {result=arRay[i];//reusult is used to store the smallest number}} return result; }}

Rotate an array to find the smallest element

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.