Features of rotating Arrays:
(1) The rotated array of the incremental sorting array can be divided into two sub-arrays;
(2) The elements of the Child array are greater than or equal to the elements of the Back-face array;
(3) The smallest element is the dividing line between two subarrays;
(4) Rotating arrays is ordered to a certain extent;
In an ordered array, binary search can be used to search for O (logn). We can also use binary search to find the minimum number of the rotated array.
Ideas:
1. Set two pointers. In the initial state, the first pointer refers to the first element of the Forward face array, and the second Pointer Points to the last element of the back face array;
2. Find the intermediate elements of the two pointers;
3. if it is greater than or equal to the element pointed to by the first pointer, it indicates that it is in the first sub-array, and obviously the smallest element is on the right of the intermediate element. If it is less than or equal to the element pointed to by the second pointer, it indicates that it is in the subarray behind it, and obviously the smallest element is on the left of the intermediate element.
In this way, you can narrow the search scope and increase the time complexity. The first pointer finally refers to the last element of the Forward face array, and the second Pointer Points to the first element of the back face array, they are adjacent, and the second Pointer Points to the smallest element.
Note: When the numbers pointed to by the two pointers and the numbers in the middle are the same, you cannot determine whether the number in the middle is in the front of the subarray or in the back of the subarray, in this case, you cannot move two pointers to narrow the search range. At this time, you can only use the sequential search method.
For example, an array {, 1} and an array {, 1} can all be regarded as the rotation of an increasing array {, 1. In the first case, the number in the middle is located in the subarray, and in the second case, the number in the middle is located in the subarray.
(5) According to the rotation rule, the first element should be greater than or equal to the last element;
But there are also special cases: if the first 0 elements of the sort array are moved to the end, and the sort array itself is still a rotation of the array, then the first number in the array is the smallest number.
C ++ code (incorrect ):
# Include "stdafx. h" # include <iostream> using namespace std; int BinarySearch_MinNumInRotateArr (int * nArr, int nLength) {if (nArr! = NULL & nLength> 0) {int low = 0; int high = nLength-1; int mid = 0; while (low + 1 )! = High) {mid = low + (high-low)> 1 ); if (nArr [low] = nArr [high] & nArr [low] = nArr [mid]) {int min = nArr [low]; for (int I = low + 1; I <= high; I ++) {if (min> nArr [I]) {min = nArr [I];} return min;} if (nArr [mid]> = nArr [low]) {low = mid;} else if (nArr [mid] <= nArr [high]) {high = mid ;}return nArr [high] ;}else {cout <"The array is empty! "<Endl; return-1 ;}int _ tmain (int argc, _ TCHAR * argv []) {int nArr1 [5] = {3, 4, 5, 1, 2 }; cout <BinarySearch_MinNumInRotateArr (nArr1, 5) <endl; int nArr2 [5] = {1, 0, 1, 1}; cout <BinarySearch_MinNumInRotateArr (nArr2, 5) <endl; int nArr3 [5] = {1, 1, 0, 1}; cout <BinarySearch_MinNumInRotateArr (nArr3, 5) <endl; int nArr4 [5] = {1, 2, 5}; // The cout result is not returned correctly in the special case <BinarySearch_MinNumInRotateArr (NArr4, 5) <endl; system ("pause"); return 0 ;}# include "stdafx. h "# include <iostream> using namespace std; int BinarySearch_MinNumInRotateArr (int * nArr, int nLength) {if (nArr! = NULL & nLength> 0) {int low = 0; int high = nLength-1; int mid = 0; while (low + 1 )! = High) {mid = low + (high-low)> 1 ); if (nArr [low] = nArr [high] & nArr [low] = nArr [mid]) {int min = nArr [low]; for (int I = low + 1; I <= high; I ++) {if (min> nArr [I]) {min = nArr [I];} return min;} if (nArr [mid]> = nArr [low]) {low = mid;} else if (nArr [mid] <= nArr [high]) {high = mid ;}return nArr [high] ;}else {cout <"The array is empty! "<Endl; return-1 ;}int _ tmain (int argc, _ TCHAR * argv []) {int nArr1 [5] = {3, 4, 5, 1, 2 }; cout <BinarySearch_MinNumInRotateArr (nArr1, 5) <endl; int nArr2 [5] = {1, 0, 1, 1}; cout <BinarySearch_MinNumInRotateArr (nArr2, 5) <endl; int nArr3 [5] = {1, 1, 0, 1}; cout <BinarySearch_MinNumInRotateArr (nArr3, 5) <endl; int nArr4 [5] = {1, 2, 5}; // The result cout <BinarySearch_MinNumInRotateArr (nArr4, 5) <endl; system ("pause"); return 0 ;}
The special case array cannot be solved.
C ++ code (correct solutions to exceptions ):
# Include "stdafx. h" # include <iostream> using namespace std; int BinarySearch_MinNumInRotateArr (int * nArr, int nLength) {if (nArr! = NULL & nLength> 0) {int low = 0; int high = nLength-1; int mid = low; while (nArr [low]> = nArr [high]) {if (high-low = 1) {mid = high; break;} mid = low + (high-low)> 1 ); if (nArr [low] = nArr [high] & nArr [low] = nArr [mid]) {int min = nArr [low]; for (int I = low + 1; I <= high; I ++) {if (min> nArr [I]) {min = nArr [I];} return min;} if (nArr [mid]> = nArr [low]) {low = mid;} else if (NArr [mid] <= nArr [high]) {high = mid ;}}return nArr [mid];} else {cout <"The array is empty! "<Endl; return-1 ;}int _ tmain (int argc, _ TCHAR * argv []) {int nArr1 [5] = {3, 4, 5, 1, 2 }; cout <BinarySearch_MinNumInRotateArr (nArr1, 5) <endl; int nArr2 [5] = {1, 0, 1, 1}; cout <BinarySearch_MinNumInRotateArr (nArr2, 5) <endl; int nArr3 [5] = {1, 1, 0, 1}; cout <BinarySearch_MinNumInRotateArr (nArr3, 5) <endl; int nArr4 [5] = {1, 2, 5}; cout <BinarySearch_MinNumInRotateArr (nArr4, 5) <<Endl; int * nArr5 = NULL; cout <BinarySearch_MinNumInRotateArr (nArr5, 5) <endl; system ("pause"); return 0 ;}# include "stdafx. h "# include <iostream> using namespace std; int BinarySearch_MinNumInRotateArr (int * nArr, int nLength) {if (nArr! = NULL & nLength> 0) {int low = 0; int high = nLength-1; int mid = low; while (nArr [low]> = nArr [high]) {if (high-low = 1) {mid = high; break;} mid = low + (high-low)> 1 ); if (nArr [low] = nArr [high] & nArr [low] = nArr [mid]) {int min = nArr [low]; for (int I = low + 1; I <= high; I ++) {if (min> nArr [I]) {min = nArr [I];} return min;} if (nArr [mid]> = nArr [low]) {low = mid;} else if (nArr [mid] <= nArr [high]) {hi Gh = mid ;}return nArr [mid];} else {cout <"The array is empty! "<Endl; return-1 ;}int _ tmain (int argc, _ TCHAR * argv []) {int nArr1 [5] = {3, 4, 5, 1, 2 }; cout <BinarySearch_MinNumInRotateArr (nArr1, 5) <endl; int nArr2 [5] = {1, 0, 1, 1}; cout <BinarySearch_MinNumInRotateArr (nArr2, 5) <endl; int nArr3 [5] = {1, 1, 0, 1}; cout <BinarySearch_MinNumInRotateArr (nArr3, 5) <endl; int nArr4 [5] = {1, 2, 5}; cout <BinarySearch_MinNumInRotateArr (nArr4, 5) <endl; int * nArr5 = NULL; cout <BinarySearch_MinNumInRotateArr (nArr5, 5) <endl; system ("pause"); return 0 ;}