As in chapter 4, calculate median