C Language Learning-search by three points

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.