page61-将二分查找重写为一段面向对象的程序
1 将二分查找重写为一段面向对象的程序 (用于在整数集合中进行查找的一种抽象数据类型)
public class StaticSETofInts 【API】
StaticSETofInts(int[] a )根据 a[]中的所有值创建一个集合
boolean contains(int key) key是否存在于集合中。
【数据实现】
import java.util.Arrays;
public class StaticSETofInts {
private int[] a;
public StaticSETofInts(int[] keys){
a = new int[keys.length];
for (int i = 0; i < keys.length; i++)
a[i] = keys[i];//保护性复制
Arrays.sort(a);
}
public boolean contains(int key){
return rank(key) != -1;
}
private int rank(int key){//二分查找
int low = 0;
int high = a.length -1;
while(low <= high){ //要么存在于a[low..high]中, 要么不存在。
int mid = (low + high) /2;
if(key < a[mid]) high = mid -1;
else if(key > a[mid]) low = mid + 1;
else return mid;
}
return -1;
}
}
【典型测试用例】
public class Whitelist {
public static void main(String[] args) {
int[] w = new In(args[0]).readAllInts();
StaticSETofInts set = new StaticSETofInts(w);
while(!StdIn.isEmpty()){
int key = StdIn.readInt();
if(!set.contains(key)) System.out.println(key);
}
}
}
【打印结果】

page61-将二分查找重写为一段面向对象的程序的更多相关文章
- 二分查找c++实现
二分查找的算法原理较为简单,在此给出c++代码实现,以及代码中遇到的问题,以及解决方案: # include "iostream" using namespace std; //t ...
- Java面向对象_常用类库api——二分查找算法
概念:又称为折半查找,优点是比较次数少,查找速度快,平均性能好:缺点是要求待查表为有序表,且插入删除困难.因此,折半查找方法适用于不经常变动而查找频繁的有序列表. 例: public class Bi ...
- 自学Python3.6-算法 二分查找算法
自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...
- StringBuffer、StringBuilder、冒泡与选择排序、二分查找、基本数据类型包装类_DAY13
1:数组的高级操作(预习) (1)数组:存储同一种数据类型的多个元素的容器. (2)特点:每个元素都有从0开始的编号,方便我们获取.专业名称:索引. (3)数组操作: A:遍历 public stat ...
- minix中二分查找bsearch的实现
在看minix中bsearch实现的源代码之前,先学习一下C 语言中void类型以及void*类型的使用方法与技巧. void的含义: void的字面意思是“无类型”,void *则为“无类型指针”, ...
- java二分查找法
//二分查找法.必须有前提:数组中的元素要有序. public static int halfSeach_2(int[] arr,int key){ int min,max,mid; min = ; ...
- java面试记录二:spring加载流程、springmvc请求流程、spring事务失效、synchronized和volatile、JMM和JVM模型、二分查找的实现、垃圾收集器、控制台顺序打印ABC的三种线程实现
注:部分答案引用网络文章 简答题 1.Spring项目启动后的加载流程 (1)使用spring框架的web项目,在tomcat下,是根据web.xml来启动的.web.xml中负责配置启动spring ...
- 零基础学习java------day12------数组高级(选择排序,冒泡排序,二分查找),API(Arrays工具类,包装类,BigInteger等数据类型,Math包)
0.数组高级 (1)选择排序 它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的起始位置 ...
- jvascript 顺序查找和二分查找法
第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...
随机推荐
- How to check “hasRole” in Java Code with Spring Security?
From:http://stackoverflow.com/questions/3021200/how-to-check-hasrole-in-java-code-with-spring-securi ...
- POJ 1005(累加)
/* * POJ_1005.cpp * * Created on: 2013年10月14日 * Author: Administrator */ #include <iostream> # ...
- VC++中几种字符标志的解释
VC++中几种字符标志的解释 LPSTR = char * LPCSTR = const char * LPWSTR = wchar_t * LPCWSTR = const wchar_t * LPO ...
- js中的call及apply
http://www.zhihu.com/question/20289071 func1.call(this, arg1, arg2); 或者 func1.apply(this, [arg1, arg ...
- JavaScript 各种遍历方式详解,有你不知道的黑科技
http://segmentfault.com/a/1190000003968126 为了方便例子讲解,现有数组和json对象如下 var demoArr = ['Javascript', 'Gulp ...
- Linux下MySQL5.6的修改字符集编码为UTF8
一.登录MySQL查看用SHOW VARIABLES LIKE 'character%';下字符集,显示如下: +--------------------------+---------------- ...
- uva539 The Settlers of Catan
The Settlers of Catan Within Settlers of Catan, the 1995 German game of the year, players attempt to ...
- linux下oracle11g R2的启动与关闭监听、数据库
su - oracle 切换到oracle账户 lsnrctl start 启动监听 sqlplus /nolog 登陆sqlplus conn /as ...
- EntityFramework Code First 优化-IIS 8的第一次优化请求配置
首先需要在Window中添加Application Initialization Application Initialization 在IIS中配置Application Pool 编辑Applic ...
- Codeforces Round #339 (Div. 2) B. Gena's Code 水题
B. Gena's Code 题目连接: http://www.codeforces.com/contest/614/problem/B Description It's the year 4527 ...