二分法查找
//非递归
private static int sort1(int[] a, int key) {
int head=0;
int end=a.length-1;
while(head<=end) {
int mid=head+(end-head)/2;
if(key>a[mid])
head=mid+1;
else if(key<a[mid])
end=mid-1;
else
return mid;
}
return -1;
}
//递归
private static int sort2(int[] a, int head, int end, int key) {
if(head>end)
return -1;
int mid=head+(end-head)/2;
if(a[mid]>key) return sort2(a,head,mid-1,key);
else if(a[mid]<key) return sort2(a,mid+1,end,key);
else return mid;
}