1. Topic Introduction
The minimum value for a rotated array. (Moves an array from the first element to the end of the array, which is the rotation array.) )
- Input: A rotation that increments the sorted array
- Output: Minimum value of the array
- Example: array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum value of this array is 1.
2. Analysis of Ideas
The most straightforward solution is to traverse sequentially from start to finish, and the time complexity of this method is O (n). The knowledge of rotating arrays is not used here and is obviously not test instructions.
In conjunction with the proposal, the rotated array is derived from an ordered array, and the two parts obtained after rotation are also ordered. Finding the preferred dichotomy in an ordered array can turn the complexity of time into O (n). Since the number of elements at the beginning of the increment array is moved to the end, the elements at the beginning of the array must be elements greater than or equal to the end of the rotation, which is the condition of the loop.
As with the dichotomy, set three pointers, low, mid, and high, pointing to the beginning, middle, and end elements of the array, respectively. If the beginning element is less than or equal to the middle element, the first half is incremented, the minimum value to be found is in the second half, and the low point is at mid, and similarly, if the middle element's point is less than or equal to the last element's value, the following is the increment sequence, the element to be found in the first half, and the
So when do we find it? When low and high are adjacent, and high refers to the minimum value of the element.
Example:
arr = {3,4,5,1,2}, low=0,high=4;mid=2;
After finding,arr[low]<arr[mid]; to prove that the first half of the sequence is incremented, the value to find is in the second half.
So make low=mid=2;mid=3, second find, Arr[mid]=1,arr[high]=2,arr[mid]<arr[high], to find in the latter part
So make high = mid = 3;
Since low and high are already adjacent, find the element and jump out of the loop
Note: There are two special cases to consider
3. Code
/* Use the dichotomy to find the minimum value that returns the rotated array, with a time complexity of O (n) */public static int Getminofroatearray (int[] Arr,int len) throws Exception { int min=0; int low = 0; int high = len-1; int mid = low; if (arr==null| | len==0) {throw new Exception ("The array is illegal"); } while (Arr[low]>=arr[high]) {if (high-low==1) {////adjacent to find mid = high; Break } mid = (Low+high)/2; In special cases, the values in the middle are equal, only the order looks for if (Arr[low]==arr[high] && arr[mid] = = Arr[low]) {min = arr[0]; for (int i = 1; i < Len; i++) {if (arr[i]<min) { min = Arr[i]; }} return min; }//Two-way if (Arr[low]<=arr[mid]) {low = Mid; }else if (Arr[mid]<=arr[high]) {high = mid; }} min = Arr[mid]; return min; }
4. Related-two points find
Array is an integral type to sort/** non-recursive */public int binarysearch (int x) {if (Array.isEmpty ()) {return-1; } int low = 0; int high = Array.size ()-1; while (low <= high) {int mid = (int) (high+low)/2; if (x = = Array.get (mid)) {return mid+1; } else if (x <array.get (mid)) {high = mid-1; } else if (x >array.get (mid)) {low = mid+1; }} return-1; /** Recursive Call * */public int BINARYSEARCHR (int x, int. low, Int. high) {int mid = (Low+high)/2; if (x < Array.get (low) | | X > Array.get (high-1) | | Low >high) {return-1; } if (X < Array.get (mid)) {return BINARYSEARCHR (X,low, mid-1); } else if (X > Array.get (mid)) {return binarysearchr (X,mid+1,high); } else {return mid+1; } }
Find minimum value in rotated array-Offer11