Skip to content

Instantly share code, notes, and snippets.

@dmjio
Created Feb 27, 2012
Embed
What would you like to do?
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