n data is described with an array a, and the lookup object is described by X.
We can compare n data to a Lookup object in turn, and we may or may not find it. This is a sequential lookup method, please the reader programming implementation.
More than sequential lookup is the binary lookup, or the second-order lookup method. Binary lookup requires n data to be sorted in order to find it quickly. Assume that n data has been sorted from small to large. The data found is described with its subscript k. Whether to find a description with a flag variable flag.
The lookup problem is converted to K in the interval [o,n 11]. First compute the point D, if A[d] an x, then k-d; if a[d]>x, the lookup interval shrinks to [o,d], and if a[d]<x, the lookup interval shrinks to [D,n 11]. Either find, or look for the interval to shrink by half, continue binary lookup.
The procedure is as follows:
float serach(a,n,x)/*折半查找函数*/
float a[],x;
int n:
{int k,flag;
int b=O,e=n一1,d;
flag=O;
do
{d=(b+e)/2;
if(a[d]==x){k=d;flag=1;}
else if(a[d]>x)e=d;
else b=d:
)while(b<e&&!flag);
if(flag==O)k=O;/*没找到*/
return(k);
}