/* * 【二分查找要求】:1.必须采用顺序存储结构 2.必须按关键字大小有序排列。 * */ public class BinarySearchDemo { public static void main(String[] args) { int[] src = new int[] {1, 3, 5, 7, 8, 9}; System.out.println(binarySearch(src, 3)); } private static int binarySearch(int[] src, int des) { int low = 0; int high = src.length - 1; while ( low <= high ) { int middle = ( low + high ) / 2; if ( des == src[middle]) { return middle; } else if ( des < src[middle]) { high = middle - 1; } else { low = middle + 1; } } return -1; } }