折半查询

 public int Search(int[] array, int key)
    {
        int low = 0;
        int high = array.Length;
        int middle;
        while (low <= high)
        {
            middle=(low+high)/2;
            if (key == array[middle])
                return middle;
            else if (key < array[middle])
                high = middle - 1;
            else
                low = middle + 1;
        }
        return -1;
    }

 

posted @ 2008-10-23 10:57  TONYBINLJ  阅读(229)  评论(0编辑  收藏  举报