SharedL1-EX11.ipynbOpen in CoCalc
L1-E11
def kShortest(arr, k):
    # builtin sort uses Timsort, which is a sorting algorithm derived from merge sort
    # best case     : O(n)
    # average case  : O(nlogn)
    # worst case    : O(nlogn)
    arr.sort()

    return arr[:k]
arr = [1,7,6,3,9,5]
k = 3

kShortest(arr, k)
[1, 3, 5]