At the beginning of this textbook, the first binary search will not work, because the author provides some common methods, need to use the jar package he provided, but also need to use the test data he provided, in order to facilitate the management of the Code, I use Ecilpse to implement and debug the algorithm, the following is mainly about the preparation of the environment.
A. Environment Preparation
Download the required jar packages and data on the website address , download the jar package and a data set of the compressed package, as
Two. Create a project
2.1 First configure the environment variables, the download of the jar package, configured in the Classpath
2.2 Create a plain Java project and import the jar package just now
As well as the extracted data, the overall directory structure is as follows:
2.3 Write the test code, mainly binary search method, placed under the defined package name
Importjava.util.Arrays;Importedu.princeton.cs.algs4.In;Importedu.princeton.cs.algs4.StdIn;ImportEdu.princeton.cs.algs4.StdOut; Public classTest { Public Static voidMain (string[] args) {int[] whilelist = In.readints (args[0]); Arrays.sort (whilelist); while(!Stdin.isempty ()) { intKey =Stdin.readint (); if(Rank (key,whilelist) <0) {stdout.println (key); } } } Public Static intRankintKeyint[] a) { intLo = 0; inthi = a.length-1; while(Lo <=hi) { intMid = lo + (Hi-lo)/2; if(Key < A[mid]) Hi = mid-1 ; Else if(Key > A[mid]) lo = mid + 1; Else returnmid; } return-1; }}
View Code
Note: The Stdin,stdout method is the author's own custom standard input and output method, according to the function name can know the specific want to implement the function.
2.4 Testing
Because many of the code in this book need to be redirected (this appendix describes redirects and pipelines in this book), such as two-point lookups, the query is given in Java test TinyW.txt < TinyT.txt, which is tested in eclipse as follows:
Check execute class Run as---run Configurations Java application (execution class below)-->common
Input file: Go to select files to redirect (files after <) Note: The file path here requires a full path
Arguments: Enter the parameters accepted by the AGRS array in the main function, and the path address can be a relative address.
Appendix: redirects and Pipelines
Two examples in this book (generating random numbers and calculating averages):
1. Redirect:
Java randomseq 100.0 200.0 > Data.txt ====> Home execution results are output to stdout.print files via standard output data.txt (), and are not printed in the console
Java Average < Data.txt ===> reads the contents of the Data.txt file through the standard input stdin and passes it to the Average class for operation.
2. Piping: The output of one program as input to another program
Java randomseq 100 100.0 200.0 | Java Average ===> The 100 averages generated by the RANDOMSEQ program most Average the parameters of the average calculation (the actual order in which these actions occur depends on the operating system)
I will implement all the algorithms in the final textbook and submit it to GitHub. Project Address: Https://github.com/yohzhangxin/Algorithms
One. "Algorithm Fourth edition" Environment construction