public class BinarySort {

	public static void main(String[] args) {
		
		int[] array = {1,2,4,6,8,12,15,23};
		System.out.println(binarysort(array,16));
	}
	
	private static int binarysort(int[] array,int target){
		
		int low = 0;
		int high = array.length - 1;
		while(low <= high){
			
			int middle = ( high + low ) / 2;
			if(array[middle] == target){
				return middle;
			}else{
				if(array[middle] < target){
					low = middle + 1;
				}else{
					high = middle - 1;
				}
			}
		}
		return -1;
	}
	
	

}

 

posted on 2013-10-05 15:31  真心的不懂  阅读(209)  评论(0编辑  收藏  举报