Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Created March 4, 2014 06:42
Show Gist options
  • Select an option

  • Save rayjcwu/9341439 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9341439 to your computer and use it in GitHub Desktop.
public int binarySearch(int []num, int target) {
int min = 0;
int max = num.length - 1;
while (min <= max) {
int mid = min + (max - min)/2;
if (num[mid] == target) {
return mid;
} else if (target < num[mid]) {
max = mid - 1;
} else {
min = mid + 1;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment