Answer the question
In order to leave comments, you need to log in
How to run programs from Algorithms in Java book?
I started studying the book "Algorithms in Java", I ran into the question of how to run programs through the terminal?
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import java.util.Arrays;
public class BinarySearch {
private BinarySearch() {
}
public static int indexOf(int[] a, int key) {
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;
}
@Deprecated
public static int rank(int key, int[] a) {
return indexOf(a, key);
}
public static void main(String[] args) {
In in = new In(args[0]);
int[] whitelist = in.readAllInts();
Arrays.sort(whitelist);
while (!StdIn.isEmpty()) {
int key = StdIn.readInt();
if (BinarySearch.indexOf(whitelist, key) == -1)
StdOut.println(key);
}
}
}
Answer the question
In order to leave comments, you need to log in
You must save the code in a file with a .Java extension.
The file name must match the class name.
Next, in the terminal, you need to go to the directory with this file and run javac filename.
Then run java classname.
For all this to work, you need the JDK. For example https://openjdk.java.net/
You can also download some IDE, for example, Intellij IDEA, everything will be built-in there, there will be syntax highlighting and convenient launch not from the console.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question