Skip to content

Instantly share code, notes, and snippets.

@Cee
Created October 31, 2014 18:01
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 Cee/526d4e89d84b3ba22306 to your computer and use it in GitHub Desktop.
Save Cee/526d4e89d84b3ba22306 to your computer and use it in GitHub Desktop.
BinarySearch
int search(int array[], int n, int value) {
int left = 0;
int right = n - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (array[mid] > value) {
right = mid - 1;
} else if (array[mid] < value) {
left = 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