Skip to content

Instantly share code, notes, and snippets.

@cocodrips
Created July 5, 2013 21:24
Show Gist options
  • Save cocodrips/5937351 to your computer and use it in GitHub Desktop.
Save cocodrips/5937351 to your computer and use it in GitHub Desktop.
二分探索 while版
int binarySearch(int[] array, int x){
int low = 0;
int high = array.length-1;
int mid;
while (low <= high) {
mid = (low + high) / 2;
if(array[mid]< x){
low = mid + 1;
}else if(array[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