二分查找法的简单使用

public class demo{
	
	 public static void main(String[] args) {
		int[] num = {1, 3, 4, 5, 6, 10};		
		boolean found;
		found = binarySearch(num, 0, 4, 5);
		System.out.println(found);
		
	} 
	 public static boolean binarySearch(int[] num, int min, int max, int target){
		 boolean find = false;
		 int midPoint = (min + max)/2;
		 if(num[midPoint] == target)
			 find = true;
		 else if(num[midPoint] < target){
			 { if(midPoint + 1 <= max)
				 find = binarySearch(num, midPoint + 1, max, target); 
			 }
			
		 }
		 else if(num[midPoint] > target)
		 { if(min <= midPoint - 1)
			 find = binarySearch(num, min, midPoint - 1, target );
		 }
		return find;
		
		 
	 }
}

你可能感兴趣的:(二分查找法的简单使用)