图灵程序设计丛书——算法Alogrithms(第四版)书里面有关二分法查找的程序例子看不懂
想请教下谁看过谢路云翻译的《算法》一书,里面有一个二分查找的算法,用于处理“白名单”问题。程序代码:
import java.util.*; public class BinarySearch { public static void main(String[] args) { //以下语句看不懂是什么作用?想请论坛的热心人帮忙解答。 int[] whiteList = In.readInts(args[0]); Arrays.sort(whiteList); while(!StdIn.isEmpty()) { int key = StdIn.readInt(); if(rank(key,whiteList) < 0) { StdOut.println(key); } } } 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; } }