二分法查找

已排序数组,查找

int BinSearch(char[] a, char b)
    {
        int low = 0;
        int high = a.Length - 1;
        int mid;
        while (low <= high)
        {
            mid = (low + high) / 2;
            if (a[mid] == b)
                return mid;
            else if (a[mid] > b)
                high = mid - 1;
            else
                low = mid + 1;
        }
        return -1;
    }
 

你可能感兴趣的:(二分法查找)