Topic DescriptionMoving a number of the first elements of an array to the end of the array, we call it the rotation of the array.
Enter a rotation of an array that is not descending sort, 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 of the elements given are greater than 0, and if the array size is 0, return 0.
Thought reference: http://blog.csdn.net/snow_7/article/details/51909764 http://blog.csdn.net/jsqfengbao/article/details/47108069
1. If the rotation occurs, the front number is at least one placed behind the array;
1) After the rotation, the array is divided into two sorted arrays, and the number in the previous array is greater than the number in the last array, because to find the smallest number in the array, which is the first number in the second array, you can use the idea of binary lookup;
2 set two pointers P1,p2,p1 point to the beginning of the array, that is, the beginning of the first array, the P2 point to the end of the array, which is the ending position of the second array. MID=P1+P2/2;
If mid is greater than the number of P1 points, mid in the first array, let P1 point to the number of mid, P1 pointing to the number of array 1;
If mid is less than the P2 point, mid is in the second array, P2 points to mid, and P2 still points to the number of array 2;
Mid is not the number that points to array 1, or the number that points to array 2. Point to the number of array 1, let P1 move to mid, point to array 2 let the P2 array moved to the position of mid, until P2 moved to the end of array 1, P1 moved to the beginning of the array 2, when P2 and P1 next to each other, And the minimum number is stored in the starting position of the array 2 that the P1 points to;
2. If the number of the middle position is equal to the number of the P1 position and the P2 position, it is not possible to determine which pointer to move, so we must use the sequential lookup method to find the smallest number.
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.
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.
Summary: This problem is to use two pointers, one backward, one move forward, specific how to move, through the size of the mid control, until the position of two pointers next to each other, you can find the smallest number!
You also need to consider:
Try to write in Python, practice python, and don't know how to write it in Java.
#-*-Coding:utf-8-*-"" "to move a number of the first elements of an array to the end of the array, we call it the rotation of the array.
Enter a rotation of an array that is not descending sort, 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 of the elements given are greater than 0, and if the array size is 0, return 0. Train of thought: first the given rotation of the array book locally ordered, which can be thought of using binary lookup to solve the optimization of linear lookup performance. The solution to this problem is similar to the binary lookup, set three pointers, Left,right,mid. When Mid>=left, the minimum number is on the right, and when the mid<=right is on the left. But in the left==right==mid can not be judged, only in order to find.
Boundary conditions require special treatment. "" "Def Minnumberinrotatearray (rotatearray): left = 0 #左侧的指针 right = Len (rotatearray)-1 #右侧的指针 mid = 0 #中间的指 Pin while Rotatearray[left] >= Rotatearray[right]: #当两个指针走到挨着的位置时, that is, right-left = = 1,right is the minimum number of IF Right-left = = 1:mid = Right Break mid = left + (int) (RIGHT-LEFT)/2) #如果中间位置的数既
The number equal to the left position is equal to the number of right positions if rotatearray[left] = = Rotatearray[mid] and Rotatearray[right]==rotatearray[mid]: Return Mininorder (rotatearray,left,right) #若中间位置的数大于左边位置的数, indicating that the smallest number is in the right of the mid position, leaving the left to the position of mid if Rota Tearray[mid] >= Rotatearray[Left]: leave = Mid #若中间位置的数小于右边指针位置的数, indicating that the smallest number is at the top of the mid position, let right go to mid's position elif Rotatearray[mid] & Lt Rotatearray[right]: right = Mid return Rotatearray[mid] #顺序查找数组里的最小值 def mininorder (rotatearray,left,right ): Minnum = rotatearray[left] length = Right-left for I in range (length): If Rotatearray[left+i] ;
Minnum:minnum = Rotatearray[left+i] return minnum if __name__ = = "__main__": array = [5,6,7,8,3,4] Print Minnumberinrotatearray (array)