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; } }
原文地址:https://www.cnblogs.com/java-spring/p/10792189.html
时间: 2024-11-10 15:18:46