[PTA] 数据结构与算法题目集 6-10 二分查找

Position BinarySearch(List L, ElementType X)
{
    int beg = 1;
    int end = L->Last;
    while (beg <= end)
    {
        int mid = (beg + end) / 2;
        if (L->Data[mid] == X)
            return mid;
        else if (L->Data[mid] < X)
            beg = mid + 1;
        else
            end = mid - 1;
    }
    return NotFound;
}
posted @ 2018-11-18 18:41  Ruohua3kou  阅读(753)  评论(0编辑  收藏  举报