二分搜索
来源:互联网 发布:hbo美剧推荐知乎 编辑:程序博客网 时间:2024/05/29 08:44
#ifndef _BINARY_SEARCH_H_
#define _BINARY_SEARCH_H_
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_asc_nonduplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
break;
}
else if(value < midValue){
endIndex = midIndex-1;
}
else if(value > midValue){
beginIndex = midIndex+1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,有重复数字(返回第一个相同的元素下标)。也支持无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_first_asc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
endIndex = midIndex-1;
}
else if(value < midValue){
endIndex = midIndex-1;
}
else if(value > midValue){
beginIndex = midIndex+1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,有重复数字(返回最后一个相同的元素下标)。也支持无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_last_asc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
beginIndex = midIndex+1;
}
else if(value < midValue){
endIndex = midIndex-1;
}
else if(value > midValue){
beginIndex = midIndex+1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_desc_nonduplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
break;
}
else if(value < midValue){
beginIndex = midIndex+1;
}
else if(value > midValue){
endIndex = midIndex-1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,有重复数字(返回第一个相同的元素下标)。也支持无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_first_desc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
endIndex = midIndex-1;
}
else if(value < midValue){
beginIndex = midIndex+1;
}
else if(value > midValue){
endIndex = midIndex-1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,有重复数字(返回最后一个相同的元素下标)。也支持无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_last_desc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
beginIndex = midIndex+1;
}
else if(value < midValue){
beginIndex = midIndex+1;
}
else if(value > midValue){
endIndex = midIndex-1;
}
}
return keyIndex;
}
#endif
#define _BINARY_SEARCH_H_
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_asc_nonduplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
break;
}
else if(value < midValue){
endIndex = midIndex-1;
}
else if(value > midValue){
beginIndex = midIndex+1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,有重复数字(返回第一个相同的元素下标)。也支持无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_first_asc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
endIndex = midIndex-1;
}
else if(value < midValue){
endIndex = midIndex-1;
}
else if(value > midValue){
beginIndex = midIndex+1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递增排序,有重复数字(返回最后一个相同的元素下标)。也支持无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_last_asc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
beginIndex = midIndex+1;
}
else if(value < midValue){
endIndex = midIndex-1;
}
else if(value > midValue){
beginIndex = midIndex+1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_desc_nonduplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
break;
}
else if(value < midValue){
beginIndex = midIndex+1;
}
else if(value > midValue){
endIndex = midIndex-1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,有重复数字(返回第一个相同的元素下标)。也支持无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_first_desc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
endIndex = midIndex-1;
}
else if(value < midValue){
beginIndex = midIndex+1;
}
else if(value > midValue){
endIndex = midIndex-1;
}
}
return keyIndex;
}
/**
* 从array[beginIndex,endIndex] 中查找value的下标索引。
* array按递减排序,有重复数字(返回最后一个相同的元素下标)。也支持无重复数字。
* cost是用来返回时间复杂度的。
* 返回value在array中的下标索引。-1表示没有找到。
*/
int binarySearch_last_desc_duplicate(int array[],int beginIndex,int endIndex,int value,int* cost){
int keyIndex = -1;
while(beginIndex <= endIndex){
++(*cost);
int midIndex = (endIndex - beginIndex) / 2 + beginIndex;
int midValue = array[midIndex];
if(value == midValue){
keyIndex = midIndex;
beginIndex = midIndex+1;
}
else if(value < midValue){
beginIndex = midIndex+1;
}
else if(value > midValue){
endIndex = midIndex-1;
}
}
return keyIndex;
}
#endif
0 0
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 二分搜索
- 设计模式-单例模式
- MD5加密之加密字符串
- Android应用内多进程分析和研究
- 将博客搬至CSDN
- Express开发框架的安装与配置
- 二分搜索
- 个推集成的那点事
- window系统查看端口号被占用,然后杀掉进程
- [SCOI2005]超级格雷码
- 有80个硬币,其中一个是假的比较轻给你个天平你要最少称几次?
- 计算机等级考试二级C语言(一)数据类型及其运算
- (一)mysql介绍
- struts中自定义excel类型的result的标签
- Ajax应用serialize()提交Form表单及后端取值(Java)