def pivot_median(seq, lo, hi):
    """Returns index to the median of seq[lo], seq[mid], seq[hi - 1]"""
    m = lo + (hi - lo) // 2  # middle element
    if seq[lo] < seq[m]:
        if seq[m] < seq[hi - 1]:
            return m
        elif seq[hi - 1] < seq[lo]:
            return lo
    else:
        if seq[hi - 1] < seq[m]:
            return m
        elif seq[lo] < seq[hi - 1]:
            return lo
    return hi - 1