Skip to content

Instantly share code, notes, and snippets.

@dmjio
Created February 27, 2012 06:06
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 dmjio/1921823 to your computer and use it in GitHub Desktop.
Save dmjio/1921823 to your computer and use it in GitHub Desktop.
BinarySearch
static int BinarySearch(int[] arr, int x)
{
int i = 0;
int m = 0;
int j = arr.Length - 1;
while (i < j)
{
m = ((i+j)/2);
if (x > arr[m])
i = m + 1;
else
j = m;
}
if (arr[m] == x)
return i;
else
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment