# 二分查找：如何用最省内存的方式实现查找功能

mid取值
low和high更新

``````public int bsearch(int[] a, int n, int value) {
int low = 0;
int high = n - 1;

while (low <= high) {
int mid =low + (high-low)/2;
if (a[mid] == value) {
return mid;
} else if (a[mid] < value) {
low = mid + 1;
} else {
high = mid - 1;
}
}

return -1;
}
``````

``````// 二分查找的递归实现
public int bsearch(int[] a, int n, int val) {
return bsearchInternally(a, 0, n - 1, val);
}

private int bsearchInternally(int[] a, int low, int high, int value) {
if (low > high) return -1;

int mid =  low + ((high - low) >> 1);
if (a[mid] == value) {
return mid;
} else if (a[mid] < value) {
return bsearchInternally(a, mid+1, high, value);
} else {
return bsearchInternally(a, low, mid-1, value);
}
}
``````

# 二分查找的变种

## 查找第一个等于给定值的元素

``````    //查找第一个等于给定值的元素
public static int searchFirstPosition(int[] arr,int key){
int lo = 0;
int hi = arr.length - 1;
while (lo <= hi){
int mid = lo + ((hi - lo) >> 1);
if (key > arr[mid]){
lo = mid + 1;
} else if (key < arr[mid]){
hi = mid - 1;
} else {
if (mid == 0 || arr[mid - 1] != key){
return mid;
} else {
hi = mid - 1;
}
}
}
return -1;
}
``````

## 查找最后一个值等于给定值得元素

``````    //查找最后一个值等于给定值得元素
public static int searchLastPostion(int[] arr,int key){
int lo = 0;
int hi = arr.length - 1;
while (lo <= hi){
int mid = lo + ((hi - lo) >> 1);
if (key > arr[mid]){
lo = mid + 1;
} else if (key < arr[mid]){
hi = mid - 1;
} else {
if (mid == arr.length - 1 || arr[mid + 1] != key){
return mid;
} else {
lo = mid + 1;
}
}
}
return -1;
}
``````

## 查找第一个大于给定值的元素

``````    //查找第一个大于给定值的元素3,4,6,7,10中查找大于等于5的元素，那就是6
public static int searchFirstBigOrEqualsPosition(int[] arr,int key){
int lo = 0;
int hi = arr.length - 1;
while (lo <= hi){
int mid = lo + ((hi - lo) >> 1);
if (key <= arr[mid]){
if (mid == 0 || arr[mid - 1] < key){
return mid;
} else {
hi = mid - 1;
}
} else {
lo = mid + 1;
}
}
return -1;
}
``````

## 查找最后一个小于等于给定值的元素

``````    //查找最后一个小于等于给定值的元素
public static int searchLastSmallOrEqualsPosition(int[] arr,int key){
int lo = 0;
int hi = arr.length - 1;
while (lo <= hi){
int mid = lo + ((hi - lo) >> 1);
if (key >= arr[mid]){
if (mid == arr.length - 1 || arr[mid + 1] > key){
return mid;
} else {
lo = mid + 1;
}
} else {
hi = mid - 1;
}
}
return -1;
}
``````