二分查找法
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
35
36
37
38
39
import java.util.Arrays;

public class BinarySearch {

//Find the number by changing the hight limit and lower limmit of the array.
public static int rank(int key, int[] a, Counter n) {

int lo = 0;
int hi = a.length - 1;
while(lo <= hi)
{
int mid = lo + (hi - lo) / 2;
n.increment();
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) {

//Print the number of the key that the whitelist has not.
int[] whitelist = {1, 9, 4, 7, 8, 25, 11};
int[] key = {7, 2, 3, 15};
Counter n = new Counter("n");

Arrays.sort(whitelist);
for(int i = 0; i < key.length; i++)
{
if(rank(key[i], whitelist, n) == -1)
System.out.println(key[i]);
}
System.out.println("the check times is : " + n.tally());
}

}