Search by three points:
# Include
# Define M 10
Int main (void)
{
Int front, near, mid1, mid2;
Int n;
Int found;
Int a [M] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Front = 0;
Near = M-1;
Found = 0;
Printf ("input n :");
Scanf ("% d", & n );
While (front <= near)
{
Mid1 = (near-front)/3 + front;
Mid2 = near-(near-front)/3;
If (n = a [mid1] | n = a [mid2])
{
Found = 1;
Break;
}
Else if (n <a [mid1])
Near = mid1-1;
Else if (n <a [mid1] & n> a [mid2])
{
Front = mid1 + 1;
Near = mid2-1;
}
Else
Front = mid2 + 1;
}
If (found = 1 & n = a [mid1])
Printf ("% d", n, mid1 );
If (found = 1 & n = a [mid2])
Printf ("% d", n, mid2 );
Return 0;
}
On the level I know now, the idea of searching by three points is the same as that of searching by two points, but there are two more variables than searching by two points. When reading a blog, one big brother wrote this:
The second part divides an interval into two segments with the same length, and the third part divides the interval into three segments with the same length for search.
It is often used to quickly determine the maximum value.
As we all know, the requirements of the binary algorithm are that the search sequence is monotonic, while the search sequence oriented by the triples method is: the sequence is a convex function.
I did not understand it very well. I am not very good at mathematics, but it is still possible to write code, share it, and study it later.