Skip to content

Instantly share code, notes, and snippets.

@metallurgix
Last active August 29, 2015 14:04
Show Gist options
  • Save metallurgix/068b2a388e5a58860b85 to your computer and use it in GitHub Desktop.
Save metallurgix/068b2a388e5a58860b85 to your computer and use it in GitHub Desktop.
Binary Search
int BinarySearch(int *array, int n, int key)
{
register int low = 0, high = n-1, mid;
while(low <= high)
{
mid = low + ((high-low)/2);
if(array[mid] < key)
low = mid + 1;
else if(array[mid] > key)
high = mid-1;
else
return mid;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment