简单的二分查找

package lianxi;

public class BinarySearch
{
    public static int binarySearch(int[] array,int value)
    {
        int low = 0;
        int high = array.length -1;
        int middle;
        
        while (low <= high)
        {
            middle = (low + high)/2;
            if(array[middle] == value)
            {
                return middle;
            }
            if(value < array[middle])
            {
                high = middle - 1;
            }
            if (value > middle)
            {
                low = middle + 1;
            }
        }
        return -1;
    }
    public static void main(String[] args)
    {
        int[] array = {1,2,34,56,67,78,89,100};
        
        int index = 0;
        index = BinarySearch.binarySearch(array, 78);
        System.out.println(index);
        
    }
}

 

posted on 2015-04-21 10:01  Kermit.Li  阅读(176)  评论(0编辑  收藏  举报

导航