Skip to content

Instantly share code, notes, and snippets.

@djg djg/bsearch2.c
Created Apr 30, 2010

Embed
What would you like to do?
int binarysearch(DataType t)
/* return (any) position
if t in sorted x[0..n-1] or
-1 if t is not present */
{
int l, u, m;
l = 0;
u = n-1;
while (l <= u) {
m = (l + u) / 2;
if (x[m] < t)
l = m+1;
else if (x[m] == t)
return m;
else /* x[m] > t */
u = m-1;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.