Find the nearest item of x from sorted array.

    :type array: list
    :param array: an iterable object that support inex

    :param x: a comparable value

    note: for finding the nearest item from a descending array, I recommend
    find_nearest(sorted_list[::-1], x). Because the built-in list[::-1] method
    is super fast.

    Usage::

        >>> find_nearest([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 5.1)
        5

    **中文文档**

    在正序数组中, 返回最接近x的数。

def find_nearest(sorted_list, x):
    """
    Find the nearest item of x from sorted array.

    :type array: list
    :param array: an iterable object that support inex

    :param x: a comparable value

    note: for finding the nearest item from a descending array, I recommend
    find_nearest(sorted_list[::-1], x). Because the built-in list[::-1] method
    is super fast.

    Usage::

        >>> find_nearest([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 5.1)
        5

    **中文文档**

    在正序数组中, 返回最接近x的数。
    """
    if x <= sorted_list[0]:
        return sorted_list[0]
    elif x >= sorted_list[-1]:
        return sorted_list[-1]
    else:
        lower = find_le(sorted_list, x)
        upper = find_ge(sorted_list, x)
        if (x - lower) > (upper - x):
            return upper
        else:
            return lower