# 七种二分查找问题总结

##### 1. 基础版——查找有序数组中key的下标

``````//非递归版
int binSearchIndex(int array[] , int len , int key){
int start = 0 , end = len - 1 , mid = 0;
while(start <= end){
mid = start + ((end - start ) >> 1);
if(array[mid] == key){
return mid;
}else if(array[mid] < key){
start = mid + 1;
}else{
end = mid - 1;
}
}
return -1;
}

//递归版
int binSearchIndexRecursion(int array[] , int start , int end , int key){
int mid = (start + end) >> 1;
if(array[mid] == key){
return mid;
}
if(array[mid] > key){
binSearchRecursion(array , start , mid - 1 , key);
}else if(array[mid] < key){
binSearchRecursion(array , mid + 1 , end , key);
}
}``````
##### 2. 基础版——查找有序数组中第一个大于等于key的下标

``````//非递归版本
int binSearch(int array[] , int len , int key){
int start = 0 , end = len - 1 , mid = 0;
while(start <= end){
mid = start + ((end - start) >> 1);
if(array[mid] >= key){
end = mid -1 ;
}else {
start = mid + 1;
}
}
return start < len ? start : -1;
}
//递归版本
int binSearchRecursion(int array[] , int start , int end , int len , int key){
int mid = start + ((end - start) >> 1);
if(start > end){
return start < len ? start : -1;
}
if(array[mid] >= key){
binSearchRecursion(array , start , mid - 1 , len , key);
}else{
binSearchRecursion(array , mid + 1 , end , len , key);
}
}``````
##### 3. 基础版——查找有序数组中第一个大于key的下标

``````//非递归版本
int binSearch(int array[] , int len , int key){
int start = 0 , end = len - 1 , mid = 0;
while(start <= end){
mid = start + ((end - start) >> 1);
if(array[mid] <= key){
start = mid + 1 ;
}else{
end = mid - 1;
}
}
return start < len ? start : -1;
}
//递归版本
int binSearchRecursion(int array[] , int start , int end , int len , int key){
int mid = (start + end) >> 1;
if(start > end){
return start < len ? start : -1;
}
if(array[mid] > key){
binSearchRecursion(array , start , mid - 1 , len , key);
}else{
binSearchRecursion(array , mid + 1 , end , len , key);
}
}
``````
##### 4. 扩展版——查找有序数组中key元素位置的最小下标

``````//非递归版本
int binSearch(int array[] , int len , int key){
int start = 0 , end = len - 1 , mid = 0;
int ans = -1;
while(start <= end){
mid = start + ((end - start) >> 1);
if(array[mid] >= key){
end = mid -1 ;
}else {
start = mid + 1;
}
}
return ((start < len) && (array[start] == key)) ? start : -1;
}
//递归版本
int binSearchRecursion(int array[] , int start , int end , int len , int key){
int mid = start + ((end - start) >> 1);
if(start > end){
return ((start <= len) && (array[start] == key)) ? start : -1;
}
if(array[mid] >= key){
binSearchRecursion(array , start , mid - 1 , len , key);
}else{
binSearchRecursion(array , mid + 1 , end , len , key);
}
}``````
##### 5. 扩展版——查找有序数组中key元素位置的最大下标

``````//非递归版本
int binSearch(int array[] , int len , int key){
int start = 0 , end = len - 1 , mid = 0;
while(start <= end){
mid = start + ((end - start) >> 1);
if(array[mid] <= key){
start = mid + 1;
}else {
end = mid -1 ;
}
}
return ((start >= 1) && (array[start - 1] == key)) ? start - 1 : -1;
}
//递归版本
int binSearchRecursion(int array[] , int start , int end , int len , int key){
int mid = start + ((end - start) >> 1);
if(start > end){
return ((start >= 1) && (array[start - 1] == key)) ? start - 1 : -1;
}
if(array[mid] > key){
binSearchRecursion(array , start , mid - 1 , len , key);
}else{
binSearchRecursion(array , mid + 1 , end , len , key);
}
}
``````
##### 6. 扩展版——查找有序数组中小于key元素的最大下标

``````//非递归版本
int binSearch(int array[] , int len , int key){
int start = 0 , end = len - 1 , mid = 0;
while(start <= end){
mid = start + ((end - start) >> 1);
if(array[mid] >= key){
end = mid -1 ;
}else {
start = mid + 1;
}
}
return (start >= 1) ? start - 1 : -1;
}
//递归版本
int binSearchRecursion(int array[] , int start , int end , int len , int key){
int mid = start + ((end - start) >> 1);
if(start > end){
return (start >= 1) ? start - 1 : -1;
}
if(array[mid] >= key){
binSearchRecursion(array , start , mid - 1 , len , key);
}else{
binSearchRecursion(array , mid + 1 , end , len , key);
}
}``````
##### 7. super版——查找有序数组中key元素出现的个数

``````//非递归版本
int binSearchMaxIndex(int array[] , int len , int key){
int start = 0 , end = len - 1 , mid = 0;
while(start <= end){
mid = start + ((end - start) >> 1);
if(array[mid] <= key){
start = mid + 1 ;
}else{
end = mid - 1;
}
}
return start < len ? start : -1;
}

int binSearchMinIndex(int array[] , int len , int key){
int start = 0 , end = len - 1 , mid = 0;
while(start <= end){
mid = start + ((end - start) >> 1);
if(array[mid] >= key){
end = mid -1 ;
}else {
start = mid + 1;
}
}
return start < len ? start : -1;
}
//递归版本
int binSearchRecursionMaxIndex(int array[] , int start , int end , int len , int key){
int mid = (start + end) >> 1;
if(start > end){
return start < len ? start : -1;
}
if(array[mid] > key){
binSearchRecursionMaxIndex(array , start , mid - 1 , len , key);
}else{
binSearchRecursionMaxIndex(array , mid + 1 , end , len , key);
}
}

int binSearchRecursionMinIndex(int array[] , int start , int end , int len , int key){
int mid = start + ((end - start) >> 1);
if(start > end){
return start < len ? start : -1;
}
if(array[mid] >= key){
binSearchRecursionMinIndex(array , start , mid - 1 , len , key);
}else{
binSearchRecursionMinIndex(array , mid + 1 , end , len , key);
}
}``````

PS：以上代码我已经在自己的机器上测试过，如果大家发现问题，欢迎指正～笔芯(•‾̑⌣‾̑•)✧˖°