基本原理:每次查找都对半分,但要求数组是有序的
public class Solution {
public static int BinarySearch(int[] sz,int key){
int low = 0;
int high = sz.length - 1;
while (low <= high) {
int middle = (low + high) / 2;
if(sz[middle] == key){
return middle;
}else if(sz[middle] > key){
high = middle - 1;
}else {
low = middle + 1;
}
}
return -1;
}
}