用eclipse运行算法第四版的BinarySearch
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了用eclipse运行算法第四版的BinarySearch,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3021字,纯文字阅读大概需要5分钟。
内容图文
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文件
原文:https://www.cnblogs.com/qinmin/p/12239731.html
内容总结
以上是互联网集市为您收集整理的用eclipse运行算法第四版的BinarySearch全部内容,希望文章能够帮你解决用eclipse运行算法第四版的BinarySearch所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。