二分查找法
2016-05-28 本文已影响42人
水书
import java.util.Arrays;
public class BinarySearch{
public static int rank(int key,int []a){
int max=a.length-1;
int min=0;
while(max>min){
int mid=min+(max+min)/2;
if(a[mid]>key)
max=mid-1;
if(a[mid]<key) min=mid+1;
else return mid;}
return -1;}
public static void main(String[] args){
int [] whitelist =In.readInts(args[0]);
Arrays.sort(whitelist);
whitelist(!StdIn.isEmpty()){
int key=StdIn.readInt();
if(rank(key,whitelist)<0)
StdOut.println(key);
}}}}