Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created January 19, 2016 12:34
Show Gist options
  • Save sudipto80/41e44b86588e2d46db1d to your computer and use it in GitHub Desktop.
Save sudipto80/41e44b86588e2d46db1d to your computer and use it in GitHub Desktop.
Binary Search
void Main()
{
BinarySearch(new int[]{1,3,4,5,9,10},1).Dump();
}
int BinarySearch(int[] array,int x)
{
int low = 0;
int high = array.Length - 1;
while (low <= high)
{
int mid = low + (high - low)/2;
if(array[mid]==x)
return mid;
if(array[mid]<x)
{
low = mid + 1;
}
if(array[mid]>x)
{
high = mid - 1;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment