常见算法之二分查找法
public class BinarySearch { public static void main(String[] args) { int[] a = {1,2,3,4,5,6,7,8,9}; int binarySerach = binarySerach(a, 4); System.out.println(binarySerach); } public static int binarySerach(int[] a, int key) { int low = 0; int high = a.length - 1; int mid = -1; while (low <= high) { mid = (low + high) / 2; if (key > a[mid]) { low = mid + 1; } else if (key < a[mid]) { high = mid - 1; } else { return mid; } } return mid; } }
劈天造陆,开辟属于自己的天地!!!与君共勉