def median(values):
    """
    >>> median([2, 4, 7, 6, 8])
    6
    >>> median([2, 4, 6, 8])
    5.0
    """
    sorted_values = sorted(values)
    n = len(values)
    midpoint = n // 2
    if n % 2:
        return sorted_values[midpoint]
    return ((sorted_values[midpoint - 1] + sorted_values[midpoint]) / 2)