import random def partition(data, start, end): if end <= start: return start index = random.randint(start, end) guard = data[index] while True: while data[start] < guard: start += 1 while data[end] > guard: end -= 1 if start >= end: break data[start], data[end] = data[end], data[start] return start def getKMins(data, k): if len( data ) <= k: return data start = 0; end = len( data ) - 1 index = partition(data, start, end) while index != k - 1: if index > k - 1: index = partition(data, start, index - 1) else: index = partition(data, index + 1, end) return data[ 0 : k]