【基础算法】- 2分查找

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;
	}
	
	

}
原文地址:https://www.cnblogs.com/lixusign/p/3352466.html