Sort a nearly sorted list
Every item in items is at most k positions away from where it belongs in sorted
order. Write sort_nearly(items, k) that returns the sorted list.
sort_nearly([6, 5, 3, 2, 8, 10, 9], 3) -> [2, 3, 5, 6, 8, 9, 10]
sort_nearly([1, 2, 3], 0) -> [1, 2, 3]
The point is to practise a heap of size k + 1: the smallest of the next k + 1 items
must be the next item of the output. sorted also passes; the heap version is n log k.
def sort_nearly(items, k):
...