二分查找:适合有序连续存储
代码:
public class BiSearch {
public static void main(String[] args) {
int nums[] = {-1,0,3,5,9,12};
System.out.println(search(nums, 9));
}
public static int search(int[] nums, int target) {
int low = 0,high = nums.length - 1;
while(low <= high){
int mid = (low+high)/2;
if(nums[mid] == target)
return mid;
else if(nums[mid] > target)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
}



