Algs4 BinarySearch.java

/******************************************************************************
 *  Compilation:  javac BinarySearch.java
 *  Execution:    java BinarySearch whitelist.txt < input.txt
 *  Dependencies: In.java StdIn.java StdOut.java
 *  Data files:   http://algs4.cs.princeton.edu/11model/tinyW.txt
 *                http://algs4.cs.princeton.edu/11model/tinyT.txt
 *                http://algs4.cs.princeton.edu/11model/largeW.txt
 *                http://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;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.In;




/**
 *  The BinarySearch class provides a static method for binary
 *  searching for an integer in a sorted array of integers.
 *  


 *  The rank 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 a if present; -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 a if present; -1 otherwise
     * @deprecated Replaced by {@link #indexOf(int[], int)}.
     */
    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.
     */
    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-2015, 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.

 ******************************************************************************/


------------------------------------------***********************--------------------------------------------

通过algs4.exe下载来的algs4文件夹,copy过来放在自己的用户名目录下就可以了(algs4文件夹就可以在各个计算机之间重用了)

之后在path里加上

C:\Users\username\algs4\java\bin;C:\Users\username\algs4\bin;

(两条分割线之间的内容就是algs4这本书所要搭载的java环境及其库的配置过程)

algs4文件夹:http://pan.baidu.com/s/1skK7i01

-----------------------------------------*************************---------------------------------------------

对于书上正式出现的第一个程序BinarySearch.java

把原代码的package edu.princeton.cs.algs4;注释掉;

加上这三行:

import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.In;



对于以上经验,真的是说多了都是泪啊,


//package edu.princeton.cs.algs4;

import java.util.Arrays;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.In;

这几串代码把我都搞得神经衰弱了,对java又不了解,一直相信官方的代码(没看官方的英文说明文档,他应该指明了这个问题,毕竟是大师,那个网站信息量比较大,加上自己英语不行,就没有去官网上找这方面的问题了),其实就是把官方的代码稍加修改就好了,欲哭无泪,几天的光阴就在这几个字符之间消逝了~~~~~


来自http://algs4.cs.princeton.edu/code/的启示

Java Algorithms and Clients



As of August 17, 2015, the "default package" version of algs4.jar has been replaced with a "named package" version. To access the classes in algs4.jar, you will need to use an import statement like the ones below:

import edu.princeton.cs.algs4.MinPQ;
import edu.princeton.cs.algs4.StdIn;

Note that the I/O libraries from stdlib.jar are now contained in algs4.jar, so you no longer need stdlib.jar.



在cmd下的重定向截图:


你可能感兴趣的:(Algs4)