calculate median according to its definition