using argpartition instead of argsort and avoiding the duplicate sorting of the distance matrix