import java.util.Arrays;

import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; /**
* The {@code BinarySearch} class provides a static method for binary
* searching for an integer in a sorted array of integers.
* <p>
* The <em>indexOf</em> operations takes logarithmic time in the worst case.
* <p>
* For additional documentation, see <a href="https://algs4.cs.princeton.edu/11model">Section 1.1</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class BinarySearch { /**
* This class should not be instantiated.
*/
private BinarySearch() { } /**
* Returns the index of the specified key in the specified array.
*
* @param a the array of integers, must be sorted in ascending order
* @param key the search key
* @return index of key in array {@code a} if present; {@code -1} otherwise
*/
public static int indexOf(int[] a, int key) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
// Key is in a[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < a[mid]) hi = mid - 1;
else if (key > a[mid]) lo = mid + 1;
else return mid;
}
return -1;
} /**
* Returns the index of the specified key in the specified array.
* This function is poorly named because it does not give the <em>rank</em>
* if the array has duplicate keys or if the key is not in the array.
*
* @param key the search key
* @param a the array of integers, must be sorted in ascending order
* @return index of key in array {@code a} if present; {@code -1} otherwise
* @deprecated Replaced by {@link #indexOf(int[], int)}.
*/
@Deprecated
public static int rank(int key, int[] a) {
return indexOf(a, key);
} /**
* Reads in a sequence of integers from the whitelist file, specified as
* a command-line argument; reads in integers from standard input;
* prints to standard output those integers that do <em>not</em> appear in the file.
*
* @param args the command-line arguments
*/
public static void main(String[] args) { // read the integers from a file
In in = new In(args[0]);
int[] whitelist = in.readAllInts(); // sort the array
Arrays.sort(whitelist); // read integer key from standard input; print if not in whitelist
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if (BinarySearch.indexOf(whitelist, key) == -1)
StdOut.println(key);
}
}
}

  这是代码,官网上最新的代码。课本上的有一点错误。

0.将algs4.jar添加到环境变量

我是新建了一个文件夹algs4,在里面存放了algls4.jar 和 algs4-date两个文件

将algs4.jar包所在的地址添加到classpath后面。

1.引入algs4.jar

右键点击工程文件,我这里是BinarySearch,然后选中properties.

再选中java build path, libraries。

右击classpath,点击add extern jars

打开algs4.jar所在的地址

选中algs4.jar即可。这样就导入了algs4.jar这个包.

2.使用eclipse的run as 进行重定向

注意:这里的input file地址:是要输入的文件的地址。

点击隐藏的选项卡,选中argument

在program argument中填入largeW.txt(白名单)

然后运行即可.

特别注意:

此处我的largeW.txt和largeT.txt在同一个文件夹里。也就是说代码工程文件里放了largeT.txt和largeW.txt文件

用eclipse运行算法第四版的BinarySearch的更多相关文章

  1. 算法第四版 在Eclipse中调用Algs4库

    首先下载Eclipse,我选择的是Eclipse IDE for Java Developers64位版本,下载下来之后解压缩到喜欢的位置然后双击Eclipse.exe启动 然后开始新建项目,File ...

  2. Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)

    Eclipse直接运行算法第4版例子(重定向和读取指定路径文件)   版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://b ...

  3. 算法第四版-文字版-下载地址-Robert Sedgewick

    下载地址:https://download.csdn.net/download/moshenglv/10777447 算法第四版,文字版,可复制,方便copy代码 目录: 第1章 基 础 ...... ...

  4. 二项分布。计算binomial(100,50,0.25)将会产生的递归调用次数(算法第四版1.1.27)

    算法第四版35页问题1.1.27,估计用一下代码计算binomial(100,50,0.25)将会产生的递归调用次数: public static double binomial(int n,int ...

  5. 算法第四版jar包下载地址

    算法第四版jar包下载地址:https://algs4.cs.princeton.edu/code/

  6. 算法第四版学习笔记之优先队列--Priority Queues

    软件:DrJava 参考书:算法(第四版) 章节:2.4优先队列(以下截图是算法配套视频所讲内容截图) 1:API 与初级实现 2:堆得定义 3:堆排序 4:事件驱动的仿真 优先队列最重要的操作就是删 ...

  7. 算法第四版学习笔记之快速排序 QuickSort

    软件:DrJava 参考书:算法(第四版) 章节:2.3快速排序(以下截图是算法配套视频所讲内容截图) 1:快速排序 2:

  8. 算法第四版 用eclipse实现书中UnionFind例子

    一 安装环境 直接下载algs4.exe 下载完成后C:\Users\zle 下面会有algs4 文件夹 原文: Our installer downloads, installs, and conf ...

  9. 配置《算法 第四版》的Eclipse开发环境

    1. 安装JAVA JAVA网址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 配置环境变量(我把JAVA安装在 ...

随机推荐

  1. 第二阶段scrum-5

    1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 注册登陆界面功能正在实装,前端制作完成 数据库配置中

  2. 深度解析高分Essay写作组成部分

    很多留学生在国外呆了好几个学期对于怎么写Essay还是不太了解,今天小编大致总结了以下一些技巧,相信同学们可以尝试着自己完成一篇Essay写作!一起来看看吧! 与你的导师沟通 你的导师对于你的Essa ...

  3. [题解] LuoguP6075 [JSOI2015]子集选取

    传送门 ps: 下面\(n\)和\(k\)好像和题目里的写反了...将就着看吧\(qwq\) 暴力打个表答案就出来了? 先写个结论,答案就是\(2^{nk}\). 为啥呢? 首先你需要知道,因为一个集 ...

  4. 【数据结构】C++语言无锁环形队列的实现

    无锁环形队列 1.Ring_Queue在payload前加入一个头,来表示当前节点的状态 2.当前节点的状态包括可以读.可以写.正在读.正在写 3.当读完成后将节点状态改为可以写,当写完成后将节点状态 ...

  5. hdu 1087 最长上升序列和 dp

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. UVA - 10791 Minimum Sum LCM(最小公倍数的最小和)

    题意:输入整数n(1<=n<231),求至少两个正整数,使得它们的最小公倍数为n,且这些整数的和最小.输出最小的和. 分析: 1.将n分解为a1p1*a2p2……,每个aipi作为一个单独 ...

  7. 如何生成 SSH keys, 并在 Github 或 Gitlab 等上添加密钥

    1 打开 Git Bash $ 2 输入 dir, 确认当前文件夹,并切换到想存密钥文件即pub文件的路径 $ dir 3 生成 密钥命令 ssh-keygen -t rsa -C "{ y ...

  8. VUE.js入门学习(1)-起步

    1.hello world <div id="app">{{content}}</div>var app = new Vue({ el:'#app', da ...

  9. JSP变量、方法和类的声明,JSP程序片,JSP表达式

    参考文章 http://c.biancheng.net/view/1431.html http://c.biancheng.net/view/1433.html http://c.biancheng. ...

  10. Springboot 2.x 如何解决重复提交 (本地锁的实践)

    有没有遇到过这种情况:网页响应很慢,提交一次表单后发现没反应,然后你就疯狂点击提交按钮(12306就经常被这样怒怼),如果做过防重复提交还好,否则那是什么级别的灾难就不好说了... 本文主要是应用 自 ...