Searching in C Language -- half-Lookup

Source: Internet
Author: User

Searching in C Language -- half-Lookup
Problem: In a series of ordered columns, the half-lookup method often gets twice the result with half the effort.

# Include <stdio. h> int main () {printf ("Enter the one you want to search for (1 ~ 10) number \ n "); int I; scanf_s (" % d ", & I); int arr [10] =, 9, 10}; int start = 1, end = 10, mid; while (start <= end) {mid = (start + end)/2; if (arr [mid]> I) {end = mid-1;} else if (arr [mid] <I) {start = mid + 1 ;} else {printf ("The number you are looking for is % d \ n", arr [mid]); break ;}} return 0 ;}

 


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.