-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.java
More file actions
34 lines (31 loc) · 872 Bytes
/
Copy pathBinarySearch.java
File metadata and controls
34 lines (31 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Arrays;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.In;
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 = new In(args[0]).readAllInts();
Arrays.sort(whitelist);
while(!StdIn.isEmpty()){
int key = StdIn.readInt();
if(rank(key, whitelist) == -1){
StdOut.println(key);
}
}
}
}