"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