Java实现二分查找
public class BinarySort {
public static int binarySort(int[] list, int low, int high, int key){
if(low>high)
return -1;
else{
int mid = (low+high)/2;
if(key>list[mid])
return binarySort(list, mid+1, high, key);
else if(key==list[mid])
return mid;
else
return binarySort(list, low, mid-1, key);
}
}
public static void main(String[] args) {
int [] list = {2,4,6,8,10,12};
System.out.println(binarySort(list, 0, list.length, 12));
}
}