where k is min or max, use partial selection sort for better performance