Skip to content

Instantly share code, notes, and snippets.

@yao2030
Created June 21, 2014 02:11
Show Gist options
  • Save yao2030/205f3133e12f1d4e7f71 to your computer and use it in GitHub Desktop.
Save yao2030/205f3133e12f1d4e7f71 to your computer and use it in GitHub Desktop.
int BinarySearch(int a[], int size, int p) {
int L = 0;
int R = size - 1;
while (L <= R) {
int mid = (L + R) / 2;
if (p == a[mid]) {
return mid;
} else if (p > a[mid]) {
L = mid + 1;
} else {
R = mid - 1;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment