Rearranged median calculation