我想好多人为第一个程序折腾了半天,反正我折腾了好久
个人感觉本书的代码在Linux上面运行更方便, 以下基于Ubuntu16.04 JDK1.8
经常访问的网站:https://algs4.cs.princeton.edu/home/ https://algs4.cs.princeton.edu/code/
1.去网站下载:stdlib.jar(本书的标准库), algs4.jar(本书所有代码), algs4-data(测试数据集合)
2.编辑代码 : javac BinarySearch.java 会出现一堆错误:缺少本书的标准库和一些代码更新所引起的错误.
import java.util.Arrays;
public class BinarySearch
{
public static int rank(int key, int[] a)
{
int lo = 0;
int hi = a.length - 1;
while(lo <= hi)
{
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;
}
public static void main(String[] args)
{
int [] whitelist = In.readInts(args[0]); //此处有问题
Arrays.sort(whitelist);
while(!StdIn.isEmpty())
{
int key = StdIn.readInt();
if(rank(key, whitelist) == -1)
StdOut.println(key);
}
}
}
因为本书采用自己的标准库,所以要导入相应的库或类,才能实现相应的输入输出和绘图等等,还有因为一些库和javaAPI的升级,但是书中还没有改过来,所以书中有些代码有问题,需要改动。此代码需要导入In.java StdIn.java StdOut.java 在stdlib.jar与algs4.jar都有,可以把他解压,然后把文件移动到代码的目录下,以algs4.jar为例,解压得到把里面的edu文件夹移动到BinarySearch.java目录下,并把此代码需要的测试数据从 algs4-data中拷贝到此目录 。如:tinyW.txt 与tinyT.txt largeW.txt与largeT.txt 用来进行实验。
3.编译和运行:
4.个人感觉看一下官方的代码很有必要,他会教你怎么编译代码和运行代码,需要导入哪些库和用到哪些测试数据,最重要是与以前的代码有哪些改变。以下是官方的BinarySearch.java代码:
/******************************************************************************
* Compilation: javac BinarySearch.java //编译
* Execution: java BinarySearch whitelist.txt < input.txt //运行
* Dependencies: In.java StdIn.java StdOut.java //依赖的库
* Data files: https://algs4.cs.princeton.edu/11model/tinyW.txt //所需的测试数据和下载地址
* https://algs4.cs.princeton.edu/11model/tinyT.txt
* https://algs4.cs.princeton.edu/11model/largeW.txt
* https://algs4.cs.princeton.edu/11model/largeT.txt
*
* % java BinarySearch tinyW.txt < tinyT.txt //示例
* 50
* 99
* 13
*
* % java BinarySearch largeW.txt < largeT.txt | more //示例
* 499569
* 984875
* 295754
* 207807
* 140925
* 161828
* [367,966 total values]
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.util.Arrays;
/**
* The {@code BinarySearch} class provides a static method for binary
* searching for an integer in a sorted array of integers.
*
* The indexOf operations takes logarithmic time in the worst case.
*
* For additional documentation, see Section 1.1 of
* Algorithms, 4th Edition 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 rank
* 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 //@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 not 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);
}
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/
由于正在学习java和Linux,有错误之处望君指出,不吝赐教。