提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
二进制搜索:对排序数组进行快速二分搜索
public class Main {/** nowjava - 时 代 Java 提供 **/ static int Dichotomy(int[] array, int left, int right, int key) { if (left > right) return -1; int m = (left + right) / 2; if (array[m] == key) { return m; } else { if (array[m] < key) return Dichotomy(array, m + 1, right, key); else return Dichotomy(array, left, m - 1, key); } } static int DichotomicSearch(int[] array, int key) { return Dichotomy(array, 0, array.length - 1, key); } public static void main(String[] args) { int[] v = {11, 6, 19, 112, 45, 67, 76, 80, 95}; /** 来 自 N o w J a v a . c o m **/ System.out.println("Seeking for element 6: Position " + Dic