Java 二分叉查找

来源:互联网 发布:淘宝哪家买电脑好 编辑:程序博客网 时间:2024/04/28 16:47

Java 二分叉查找


   Integer[] list = new Integer[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1};        int start = 2;        int end =9;        int low = 0;        int hight = list.length - 1;        int index = 0;        while (low < hight) {            index = (hight+low) % 2 == 0 ? (hight+low) / 2 : (hight+low + 1) / 2;            if (list[index] < end) hight = index - 1;            else if (list[index] > end) low = index + 1;            else {                System.out.println(list[index]);                break;            }        }        for (int i = index + 1; i < list.length; i++) {            if (list[i] < start) break;            System.out.println(list[i]);        }


0 0
原创粉丝点击