diff options
author | Bobby Bingham <koorogi@koorogi.info> | 2014-07-17 21:21:20 -0500 |
---|---|---|
committer | Bobby Bingham <koorogi@koorogi.info> | 2014-07-17 21:21:20 -0500 |
commit | 54d463c220a76592165dcedc4639bf6f35b7d01c (patch) | |
tree | 3a1617c44e885b675e92806ba70186a63d7ecef3 /bsearch.c | |
parent | e8da3ce84ae3ed4d9b6dacf24866e599a5df7d41 (diff) |
Factor out binary_search
Diffstat (limited to 'bsearch.c')
-rw-r--r-- | bsearch.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/bsearch.c b/bsearch.c new file mode 100644 index 0000000..3c94d45 --- /dev/null +++ b/bsearch.c @@ -0,0 +1,24 @@ +#include <stddef.h> + +#include "sorters.h" + +/* If a matching element exists, returns index of one of them. + If none exists, returns index where such an element should be inserted. */ +size_t binary_search(const char *needle, char *haystack, size_t nmel, size_t width, cmpfun cmp) +{ + size_t baseidx = 0; + while (nmel) { + size_t tryidx = baseidx + nmel / 2; + char *try = haystack + tryidx * width; + int sign = cmp(try, needle); + if (!sign) return tryidx; + else if (sign < 0) { + baseidx = tryidx + 1; + nmel -= nmel / 2 + 1; + } + else { + nmel /= 2; + } + } + return baseidx; +} |