There are many variants of the question:
1. The first method is to shift an ordered array cyclically to k bits. It takes at least a few numbers to determine the increase or decrease of the original array.
A: assume that the three numbers are abc. If the size relationship is [Bac, ACB, CBA], you can know that the original array is increasing, and vice versa.
2. Search for the starting position of the original array on an ordered array with known cyclic shift [increment or decrease according to the method in 1]
A: [if it is incremental] the binary search method is used to compare the current value with the low-end value of [low] during each search to determine which side
If s [Mid]> S [low], it indicates that the start position is in the range [mid, high ].
If s [Mid] <s [low], it indicates that the start position is in the [low, mid] range.
Int F (int * a, int I, Int J)
{
Int m = 0;
While (I <j + 2)
{
If (J-I = 1)
Return a [I] <A [J]? I: J;
M = (I + J)/2;
If (A [m]> A [I])
{
I = m;
M = (I + J)/2;
}
Else
{
J = m;
M = (I + J)/2;
}
}
}
The default array of the above program is incremental. Returns the starting position of the original array.
3. Search for a given value in an ordered array with known cyclic shift
Answer: first, we can use 1 to determine the increase and decrease of the array. If it is incremental, we can combine the current mid search value and the values at both ends to determine the range of the values to be searched. Then, we can narrow down the range, you can.
Int find (int * s, int low, int high, int m)
{
Int mid;
While (low + 1 {
Mid = low + (high-low)/2;
If (s [Mid] = m)
{
Return mid;
}
If (M> S [Mid])
{
If (s [Mid]> S [low])
{
Low = mid;
}
Else
If (M> = s [High])
{
High = mid;
}
Else
Low = mid;
}
Else
{
If (s [Mid]> S [low])
{
If (M> = s [low])
{
High = mid;
}
Else
Low = mid;
}
Else
High = mid;
}
}
If (s [low] = m)
{
Return low;
}
If (s [High] = m)
{
Return high;
}
Return-1;
}
4. If the first part of an array is incremental and the last part is also incremental, find the given value in it.
This is quite tangled
5. If the first part of an array is incremented and the last part is decreased, find the given value in it.
A: Determine the last two elements of the array, and check whether there is a decreasing part in the last part of the test. No. You can use the binary method directly.
If there is a decreasing part, search for the two parts first and then the two parts.
From: http://www.cnblogs.com/davidluo/articles/1837561.html