来源于数据结构与算法分析中的折半查找
来源:互联网 发布:我国经济数据 编辑:程序博客网 时间:2024/06/01 10:08
/**
*Performs the standard binary search.
*@return index where item is found,or -1 if not found.
*
*/
public static <AnyType extends Comparable<? super AnyType>>int binarySearch(AnyType [] a, AnyType x){
int low = 0,high = a.length - 1;
while(low <= high){
int mid = (low + high) / 2;
if(a[mid].compareTo(x) < 0)
low = mid + 1;
else if(a [mid].compareTo(x) > 0)
high = mid - 1;
else
return mid;//Found
}
return NOT_FOUND; //NOT_FOUND is defined as -1
*Performs the standard binary search.
*@return index where item is found,or -1 if not found.
*
*/
public static <AnyType extends Comparable<? super AnyType>>int binarySearch(AnyType [] a, AnyType x){
int low = 0,high = a.length - 1;
while(low <= high){
int mid = (low + high) / 2;
if(a[mid].compareTo(x) < 0)
low = mid + 1;
else if(a [mid].compareTo(x) > 0)
high = mid - 1;
else
return mid;//Found
}
return NOT_FOUND; //NOT_FOUND is defined as -1
}
//Do not know is right, please understand!!!
阅读全文
1 0
- 来源于数据结构与算法分析中的折半查找
- 数据结构与算法-查找-折半查找
- 数据结构与算法_折半查找
- 折半查找算法分析
- 数据结构与算法-折半查找(二分查找)
- <数据结构与算法>二分查找(折半查找)
- 查找算法—折半查找算法分析与实现(Python)
- 【算法分析与设计】顺序查找与折半查找
- 【数据结构与算法】折半查找算法(二分法)
- 数据结构查找算法之折半查找
- 数据结构与算法13:折半查找、差值查找和斐波那契查找
- 【数据结构与算法】【查找】折半查找(二分查找)的代码实现
- 【算法数据结构Java实现】折半查找
- 实现折半查找的算法(数据结构)
- 算法:顺序查找与折半查找
- 《数据结构与算法分析》--二叉查找树
- 数据结构与算法分析笔记与总结(java实现)--数组1:二维数组中的查找
- 【数据结构】算法9.2 有序表查找-折半查找
- Python|计蒜客——泥塑课
- 设计模式学习—备忘录模式(Memento Design Pattern)
- 适配器模式
- MySQL索引背后的数据结构及算法原理
- 内嵌函数和闭包
- 来源于数据结构与算法分析中的折半查找
- poj 3093
- [POJ](3984)迷宫问题 ---BFS+队列模拟(图+队列模拟)***
- Cloudera Manager5.11.0离线安装详细步骤
- 大整数乘法 java实现
- Linux防火墙iptables规则设置
- 关于react-native对当前系统网络的判断
- 使用表情过滤工具在魅族手机和华为手机上会报错的问题
- 关于R语言的绘图函数[转]