completed median function