The sword refers to the offer face Question 8 (Java Edition): Rotate the smallest number of arrays

Source: Internet
Author: User
Title: Move a number of the first elements of an array to the end of the array, which we call rotation. Enter a rotation of an ascending 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 smallest element of the array is 1.

The most intuitive solution to this problem is not difficult, traversing through once, we can find the smallest element. The time complexity of this idea is O (n). But this idea does not use the input of the rotation of the characteristics of the array, it is certainly not up to the requirements of the interviewer.

We notice that the rotated array can actually be divided into two sorted sub arrays, and that the elements of the preceding child array are elements that are greater than or equal to the back-face array. We also notice that the smallest element is just the dividing line of these two sub arrays. In the sorted array we can use the binary lookup to implement the O (LOGN) lookup. The arrays given are sorted to some extent, so we can try to find the smallest element with the idea of a binary lookup.

In the previous example, we first point the first pointer to the NO. 0 element and point the second pointer to the 4th element, as shown in the figure. The number that is in the middle of two pointers (the subscript of the array is 2) is 5, which is greater than the number that the first pointer points to. So the median 51 is positioned in the first ascending word group, and the smallest number must be behind it. So we can move the first pointer and let it point to the middle of the array.

The number at this point in the middle of the two pointers is 1, which is less than the number pointed to by the second pointer. So the median number 11 is positioned in the second ascending sub array, and the smallest number must be in front of it or it is the smallest number. So we can move the second pointer to the element in the middle of two pointers, which is subscript 3.

The two pointer has a distance of 1, indicating that the first pointer already points to the end of the first incrementing array, while the second pointer points to the beginning of the second incrementing child array. The first digit of the second array is the smallest number, so the number that the second pointer points to is the result we are looking for.


Whether the above method must be perfect enough. The interviewer will tell you that it's not. He will be prompted to examine the same two numbers of Leftindex and Rightindex respectively, and the corresponding P1 and P2 on the way. In the front, in the same way as two numbers, and the same number in the middle, we assign Indexmid to Leftindex, which means that the smallest number is at the back of the middle number. is not necessarily the same.

Let's look at one more example. Array {1,0,1,1,1} and arrays {1,1,1,0,1} can be called ascending sort array {0,1,1,1,1} rotation, Figure 2 respectively, they are separated by the smallest number of two sub arrays.


In both cases, the first and second pointers point to the number 1, and the number in the middle of the two pointer is 1, which is the same 3 digits. In the first case, the median (subscript 2) is in the following array, and in the second case, the middle number (subscript 2) is in the preceding child array. So when two pointers to the numbers and the numbers in between are the same, we can't tell if the middle number is in the previous sub array or in the back of the sub array China, nor can you move two pointers to narrow the search. At this point, we have to use the sequential lookup method.

After analyzing the problem and forming a clear idea, we can change the previous code to:

Java Code Implementation:

/** * Move a number of the first elements of an array to the end of the array, which we call rotation.
 * Enter a rotation of an ascending 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}, the smallest element of which is 1.

* * Package swordforoffer; /** * @author Jinshuangqi * * * July 28, 2015 * * public class E08minnumberinrotatedarray {public int mininreversinglist (i
		Nt[] Arr {if (arr==null) {return-1;
		int leftindex = 0;
		int rightindex = arr.length-1;
		int midindex = Leftindex;
				while (arr[leftindex]>= Arr[rightindex]) {if (Rightindex-leftindex <= 1) {midindex = Rightindex;
			Break
			} Midindex = (Leftindex+rightindex)/2; if (arr[leftindex]== Arr[rightindex] && arr[midindex]== Arr[leftindex]) {return Mininorder (Arr,leftindex,
			Rightindex);
			} if (Arr[midindex] >= Arr[leftindex]) {leftindex = Midindex;
			}else if (Arr[midindex] < Arr[rightindex]) {rightindex = Midindex;
	} return Arr[midindex];
		public int Mininorder (int[] arr,int leftindex,int rightindex) {int result = Arr[leftindex]; for (int i = LeftInDex +1;i<rightindex;i++) {if (result> Arr[i]) {result = Arr[i];
	} return result;
		public static void Main (string[] args) {E08minnumberinrotatedarray test = new E08minnumberinrotatedarray ();    Int[] arr={3,4,5,1,2};
		{2,2,2,2,2,0,1,2,2};
		
	System.out.println (Test.mininreversinglist (arr));
 }
}


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.