|
|
coherent
bsearch() General Function bsearch()
Search an array
#iinncclluuddee <ssttddlliibb.hh>
cchhaarr *bbsseeaarrcchh(_k_e_y, _a_r_r_a_y, _n_u_m_b_e_r, _s_i_z_e, _c_o_m_p_a_r_i_s_o_n)
cchhaarr *_k_e_y, *_a_r_r_a_y;
ssiizzee_tt _n_u_m_b_e_r, _s_i_z_e;
iinntt (*_c_o_m_p_a_r_i_s_o_n)();
bbsseeaarrcchh searches a sorted array for a given item. _i_t_e_m points to
the object sought. _a_r_r_a_y points to the base of the array; it has
_n_u_m_b_e_r elements, each of which is _s_i_z_e bytes long. Its elements
must be sorted into ascending order before it is searched by
bbsseeaarrcchh.
_c_o_m_p_a_r_i_s_o_n points to the function that compares array elements.
_c_o_m_p_a_r_i_s_o_n must return zero if its arguments match, a number
greater than zero if the element pointed to by _a_r_g_1 is greater
than the element pointed to by _a_r_g_2, and a number less than zero
if the element pointed to by _a_r_g_1 is less than the element
pointed to by _a_r_g_2.
bbsseeaarrcchh returns a pointer to the array element that matches _i_t_e_m.
If no element matches _i_t_e_m, then bbsseeaarrcchh returns NULL. If more
than one element within _a_r_r_a_y matches _i_t_e_m, which element is
matched is unspecified.
***** Example *****
This example uses bbsseeaarrcchh to translate English into ``bureaucrat-
ese''.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct syntab {
char *english, *bureaucratic;
} cdtab[] = {
/* The left column is in alphabetical order */
"affect", "impact",
"after", "subsequent to",
"building","physical facility",
"call", "refer to as",
"do", "implement",
COHERENT Lexicon Page 1
bsearch() General Function bsearch()
"false", "inoperative",
"finish", "finalize",
"first", "initial",
"full", "in-depth",
"help", "facilitate",
"kill", "terminate",
"lie", "inoperative statement",
"order", "prioritize",
"talk", "interpersonal communication",
"then", "at that point in time",
"use", "utilize"
};
int
comparator(key, item)
char *key;
struct syntab *item;
{
return(strcmp(key, item->english));
}
main()
{
struct syntab *ans;
char buf[80];
for(;;) {
printf("Enter an English word: ");
fflush(stdout);
if(gets(buf) || !strcmp(buf, "quit") == NULL)
break;
if((ans = bsearch(buf, (char *)cdtab,
sizeof(cdtab)/ sizeof(struct syntab),
sizeof(struct syntab),
comparator)) == NULL)
printf("%s not found\n");
COHERENT Lexicon Page 2
bsearch() General Function bsearch()
else
printf("Don't say \"%s\"; say \"%s\"!\n",
ans->english, ans->bureaucratic);
}
}
***** See Also *****
ggeenneerraall ffuunnccttiioonnss, qqssoorrtt(), ssttddlliibb.hh
***** Notes *****
The name bbsseeaarrcchh implies that this function performs a binary
search. A binary search looks at the midpoint of the array, and
compares it with the element being sought. If that element
matches, then the work is done. If it does not, then bbsseeaarrcchh
checks the midpoint of either the upper half of the array or of
the lower half, depending upon whether the midpoint of the array
is larger or smaller than the item being sought. bbsseeaarrcchh bisects
smaller and smaller regions of the array until it either finds a
match or can bisect no further.
It is important that the input _a_r_r_a_y be sorted, or bbsseeaarrcchh will
not function correctly.
COHERENT Lexicon Page 3
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.