winfwu

导航

二分查找

package TestFro0406;

public class DemoFro二分查找 {

        private static final int size = 5000000;
        public static void main(String[] args) {
        long[] data = new long[size];
        // 添加测试数据
        for (int k = 0; k < data.length; k++) {
        data[k] = k;
        }
        // 要查找的数据
        long target = -1;
        int f=binaryFind(data, target);
        System.out.println(f);
        }
    
        public static int binaryFind(long[] data, long target) {
        int start = 0;
        int end = data.length - 1;
        while (start <= end) {
        int middleIndex = (start + end) / 2;
        if (target == data[middleIndex]) {
        return middleIndex;
        }
        if (target >= data[middleIndex]) {
        start = middleIndex + 1;
        } else {
        end = middleIndex - 1;
        }
        }
        return -1;
        }
    }

 

posted on 2013-04-06 17:44  winfwu  阅读(144)  评论(0编辑  收藏  举报