implemented weighted median algorithm