Find the minimum value of the rotated array:
Suppose a sorted array rotates with an unknown element as a fulcrum, finding the smallest value in the rotated array, assuming there are no duplicate elements in the array.
such as: The original array 1,2,3,4,5,6,7 rotated to get 4,5,6,7,1,2,3. The minimum value after rotation is 1.
Problem Analysis:
Not too much to introduce here, after the rotation of the array can actually be divided into two ordered arrays, the size of the front sub-array is larger than the size of the rear face array.
The smallest element is the dividing line between two arrays.
Program implementation:
1 /***************************************2 FileName FindMinRotateArray.cpp3 Author:godfrey4 CREATEDTIME:2016/5/35 ****************************************/6#include <iostream>7#include <cstring>8#include <vector>9#include <algorithm>Ten#include <stdio.h> One#include <stdlib.h> A - using namespacestd; - the intFindminrotatearray (intAintsize) { - intLow =0; - intHigh = size-1; - intmid; + while(Low <High ) { -Mid = (low + high)/2; + if(A[mid] > A[high]) {//minimum value in right half ALow = mid +1; at } - Else if(A[mid] < A[high]) {//minimum value in left half -High =mid; - } - } - returnA[low]; in } - intMain () to { + intA[] = {4,5,6,7,1,2,3}; - intFindminnum = Findminrotatearray (A,sizeof(a)/sizeof(int)); thecout<<"The findminnum:"; *cout<<findminnum<<Endl; $ return 0;Panax Notoginseng}
Operation Result:
Reprint please specify the source:
C + + Blog Park: godfrey_88
http://www.cnblogs.com/gaobaoru-articles/
Find the minimum value of a rotated array