Thought: The biggest feature of a circular ordered array is that when binary search is used, one side is always ordered. With this feature, value is used to store the historical minimum value.
When the order on the left is obtained, use a [low] to compare with the value to obtain the minimum value on the current left, and then jump to the right to see if there is a smaller value;
If the order is on the right, compare a [Mid] with value to get the minimum value on the right, and then jump to the left to check whether there are smaller values.
The algorithm complexity is O (logn)
The Code is as follows:
# Include <iostream> # include <stdio. h> # include <assert. h> using namespace STD; int binary_search (int * a, int size) {assert (! = NULL); int low = 0; int value = A [0]; // initialize int high = size-1; while (low <= high) {int mid = (low + high)/2; if (a [low] <= A [Mid]) // left ordered, note the use of '=' {if (a [low] <value) // compare with the minimum value of the Order on the left {value = A [low]; Low = Mid + 1; // jump to the right and try to find the minimum value} else low = Mid + 1;} else // right ordered {if (a [Mid] <value) // compare with the minimum ordered value on the right {value = A [Mid]; high = mid-1; // jump to the left and try to find the minimum value} elsehigh = mid-1 ;}} return Value;} int main () {int A [10] = {,}; cout <binary_search (A, 10 ); return 0 ;}
The binary search idea is to find the minimum value in an ordered array.