二分查找BinarySearch

public class BinarySearch {

	public static void main(String[] args) {
		int[] arr = {1,3,4,5,6,7,8,9};
		System.out.println("location = "+binarySearch(arr,2));

	}
	
	public static int binarySearch(int[] arr,int x)
	{
		int low = arr[0];
		int high = arr[arr.length-1];
		for(int i=0;imiddle)
			{
				low = middle+1;
			}
			else
			{
				high = middle-1;
			}
		}
		return -1;
	}

}

你可能感兴趣的:(算法)