Created
July 5, 2013 21:24
-
-
Save cocodrips/5937351 to your computer and use it in GitHub Desktop.
二分探索 while版
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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