Recursive rewriting of binary lookups

Source: Internet
Author: User

"Problem description" a recursive binary lookup algorithm is written for static lookup tables represented by non-incrementing ordered tables.

"Input form" The static lookup table from subscript 1 to store data, storage data in a non-incremental order, the specific input form as follows:

5//number of input elements

33 29 25 20 12//Enter multiple numbers consecutively in a non-ascending order, separated by a space between each number

29//Enter the number you want to find
"Output form" outputs the subscript of the data in the lookup table if found, and outputs 0 if it is not found

If find 29, it will output 2
"Sample Input" 5

33 29 25) 20 12

29

"Sample Output" 2

#include <stdio.h>intSearchintKey[],intLowintHighintk) {    intmid; if(high<Low )return-1; Else{mid= (Low+high)/2; if(key[mid]==k)returnmid; if(k>Key[mid])returnSearch (key,low,mid-1, K); Else        returnSearch (key,mid+1, high,k); }}intMain () {intN,i,p,add; inta[ -]; scanf ("%d",&N);  for(i=1; i<=n;i++) {scanf ("%d",&A[i]); } scanf ("%d",&p); Add=search (A,1, n,p); if(-1!=add) printf ("%d", add); Elseprintf ("0"); return 0;}

Recursive rewriting of binary lookups

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.