码迷,mamicode.com
首页 > 其他好文 > 详细

二分查找

时间:2014-11-25 12:36:24      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   java   for   

java

import java.util.Arrays;
import java.util.Random;

public class Solution {

    public void demo(int n) {
        if (n < 2)
            return;
        int[] array = new int[n];
        Random rand = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = rand.nextInt(1000);
        }
        Arrays.sort(array);
        int value = array[rand.nextInt(n)];
        for (int i = 0; i < array.length; i++) {
            value = array[i];
            System.out.println("the number needs to be found:" + value);
            System.out.println("the position of value is:"
                    + binSearch(0, array.length - 1, value, array));
            System.out.println("the position of value is:"
                    + binSearch1(value, array));
            System.out.println("the position of value is:"
                    + binSearch2(0, array.length - 1, value, array));
        }
        System.out.println("**************");
        for (int i = 0; i < array.length; i++) {
            System.out.println(i + ":" + array[i]);
        }
        System.out.println("**************");
    }

    // 递归
    public int binSearch(int low, int high, int value, int[] array) {
        int mid = (low + high) / 2;
        if (mid > 0 && mid < array.length - 1) {
            if (array[mid] == value)
                return mid;
            else if (array[mid] < value) {
                return binSearch(mid + 1, high, value, array);
            } else {
                return binSearch(low, mid - 1, value, array);
            }
        }
        if (array[low] == value)
            return low;
        if (array[high] == value)
            return high;
        return -1;
    }

    // 非递归
    public int binSearch1(int value, int[] array) {
        int mid;
        int low = 0;
        int high = array.length - 1;
        while (low <= high) {
            mid = (low + high) / 2;
            if (array[mid] == value) {
                return mid;
            } else if (array[mid] > value) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }

    // 改进递归
    public int binSearch2(int low, int high, int value, int[] array) {
        if (low > high)
            return -1;
        int mid = low + (high - low) / 2;
        if (array[mid] == value)
            return mid;
        else if (array[mid] < value) {
            return binSearch2(mid + 1, high, value, array);
        } else {
            return binSearch2(low, mid - 1, value, array);
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Solution s = new Solution();
        s.demo(13);
    }

}

 

二分查找

标签:style   blog   io   ar   color   os   sp   java   for   

原文地址:http://www.cnblogs.com/touc/p/4120620.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!