The start of the rotation array is the loop left of the array element or the loop right. For example: {3,4,5,1,2} is a rotating array of {1,2,3,4,5}. The rotated array is based on a non-descending array, so the array should have been ordered.
ordered array to rotated array
If you really use the loop left or loop right to achieve the rotation of the array, then there will be a large number of elements to move, so a good way to minimize the movement of elements in the array.
Method:
1. Reverse-order the first element of the array 2. Reverse-order The latter element of the array 3. All elements of an array are reversed in reverse order using two pointers, a head pointer to a tail pointer, all moving in the middle, an exchange reverse.
Rotate the minimum value of an array
Finding the minimum value can be traversed once, with a time complexity of O (n), but also a faster algorithm to look at the characteristics of the rotating array:
1. The rotated array is partially ordered 2. The previous section > The following section 3. The minimum element appears at the boundary of two parts
Depending on the order of the parts of the rotated array, binary can be used to find.
Two pointers define the range to look for, start and End1, respectively. If the rotation array equals the original array, that is, no elements are looped around, that is, start corresponds to an element that is smaller than the end corresponding element. Then the first element is the smallest element. 2. Binary find termination Condition: If end-start=1, then end corresponding element is the smallest element 3. Take the intermediate element to mid= (start+end)/2 If the value of mid corresponds to the value of start corresponding to the values of end, then this is the case: { 1,0,1,1,1,}, it is not possible to determine the corresponding 1 in table 2, the first half of the ascending or the second half of ascending. Binary find invalid, use order to find. If mid corresponds to a value that is greater than or equal to the value of start, the value for mid is still in the first ascending part, then Start=mid (cannot be equal to mid+1, because the terminating condition is 1 apart) if mid corresponds to a value less than or equal to the end value, That means the value of mid corresponds to the second ascending part, then End=mid (not equal to mid-1, because mid may be the smallest element)
Code implementation:
int Minnumberinrotatearray (vector<int> rotatearray) {if (rotatearray.size () = = 0) return 0; int index1 = 0; int index2 = Rotatearray.size ()-1; int indexmid = index1; The array element has a cyclic movement of 0 if (Rotatearray[index1] < ROTATEARRAY[INDEX2]) return ROTATEARRAY[INDEX1]; while (Index1 < INDEX2) {if (index2-index1 = = 1) {Indexmid = i Ndex2; Break }//if Indexmid = (index1 + index2)/2; Conditions for sequential lookups if (rotatearray[index1] = = Rotatearray[index2] && rotatearray[index1] = = Rotatearray[indexmid] ) {return Mininorder (Rotatearray, index1, INDEX2); } if (Rotatearray[indexmid] >= rotatearray[index1]) {index1 = Indexmid ; } else if (Rotatearray[indexmid] <= RotateaRRAY[INDEX2]) {index2 = Indexmid; }}//while return Rotatearray[indexmid]; }//Order lookup int Mininorder (vector<int>& nums, int index1, int index2) {int result = Nums[index 1]; for (int i = index1 + 1; I <= index2; i++) {if (Result > Nums[i]) {Resul t = nums[i]; }} return result; }
Pay great attention to this problem. Test Case:
1. The number of elements in the input array is 02. The array has only one element 3. An array of ascending order (with a circular movement of 0) 4. Loop array with repeating elements (sequential lookup)
[Array] Rotation array