From: Dan Cross <cross@math.psu.edu>
To: 9fans@cse.psu.edu
Subject: Re: [9fans] bsearch ?
Date: Mon, 30 Jun 2003 09:57:20 -0400 [thread overview]
Message-ID: <200306301357.h5UDvK707520@augusta.math.psu.edu> (raw)
In-Reply-To: Your message of "Mon, 30 Jun 2003 12:02:02 BST." <6d4520144abb8c805ef4d14c3b919320@snellwilcox.com>
> bsearch(2) is not in libc.
>
> I am sure it will not be for a good reason, but why?
> qsort is there so why not its friend?
Probably because it's not used that much. Regardless, I once
wondered this myself and wrote a version. Since binary search
is for some inexplicable reason one of those things that seems
is impossibly hard to get right, I'll paste it below; it
probably would be good to stick it in libc.
- Dan C.
void *
bsearch(void *key, void *base, long nel, long width, int (*cmp)(void *, void *))
{
char *bp;
long m, l, r;
int c;
for (bp = base, l = 0, r = nel - 1; l <= r; ) {
m = (l + r) / 2;
c = cmp(key, bp + m * width);
if (c == 0)
return(bp + m * width);
else if (c < 0)
r = m - 1;
else
l = m + 1;
}
return(nil);
}
next prev parent reply other threads:[~2003-06-30 13:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-06-30 11:02 steve.simon
2003-06-30 13:57 ` Dan Cross [this message]
2003-06-30 14:15 Trickey, Howard W (Howard)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200306301357.h5UDvK707520@augusta.math.psu.edu \
--to=cross@math.psu.edu \
--cc=9fans@cse.psu.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).