Question
I am working on IntelliJ IDEA, I am trying to get this to work. I have tried a couple different things but it still hasnt
I am working on IntelliJ IDEA, I am trying to get this to work. I have tried a couple different things but it still hasnt worked.
****************************************************************************** * 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] * ******************************************************************************/
import java.util.Arrays;
/** * The BinarySearch class provides a static method for binary * searching for an integer in a sorted array of integers. */ public class BinarySearch {
/** * This class should not be instantiated. */ private BinarySearch() { }
/** * Returns the index of the specified key in the specified array. * The array of integers, must be sorted in ascending order */ public static int indexOf(int[] a, int key) { int low = 0; int high = a.length - 1; while (low <= high) { // Key is in a[low..high] or not present. int middle = low + (high - low) / 2; if (key < a[middle]) high = middle - 1; else if (key > a[middle]) low = middle + 1; else return middle; } return -1; }
/** * Returns the index of the stated key in the stated array. *
* if the array has duplicate keys or if the key is not in the array. */ @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; */ 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); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started