Skip to content

Instantly share code, notes, and snippets.

@danielrobertson
Created July 10, 2016 23:12
Show Gist options
  • Save danielrobertson/308d1d226c379af8d61a79050c496d24 to your computer and use it in GitHub Desktop.
Save danielrobertson/308d1d226c379af8d61a79050c496d24 to your computer and use it in GitHub Desktop.
Binary Search
int binarySearch(int[] a, int x) {
int low = 0;
int high = a.length - 1;
int mid;
while (low <= high) {
mid = (low + high)
if (a[mid] < x) {
low = mid + 1;
} else if(a[mid]>x){
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