Skip to content

Instantly share code, notes, and snippets.

@djg
Created April 30, 2010 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djg/384775 to your computer and use it in GitHub Desktop.
Save djg/384775 to your computer and use it in GitHub Desktop.
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