Half-lookup written in pure C Language

Source: Internet
Author: User

# Include
 
  
Int bin_search (int key [], int low, int high, int k) {int mid; if (low> high) return-1; else {mid = (low + high) /2; if (key [mid] = k) return mid; if (k> key [mid]) return bin_search (key, mid + 1, high, k ); /* search for the second half of the sequence */else return bin_search (key, low, mid-1, k ); /* search for the first half of the sequence */} int main () {int n, I, addr; int A [10] =, 19,21}; printf ("The contents of the Array A [10] are \ n"); for (I = 0; I <10 ; I ++) printf ("% d", A [I]); /* display the content in array A */printf ("\ nPlease input a interger for search \ n"); scanf ("% d", & n ); /* enter the element to be searched */addr = bin_search (A, n); if (-1! = Addr)/* search successful */printf ("% d is at the % dth unit is array A \ n", n, addr); else
 
Printf ("There is no % d in array A \ n", n);/* search failed */getchar (); 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.