import heapq
def sortKSortedArray(array, k):
index = 0
heap = array[:k+1] # 0~k
heapq.heapify(heap)
for i in range(k+1, len(array)):
array[index] = heapq.heappop(heap)
index += 1
heapq.heappush(heap, array[i])
while heap:
array[index] = heapq.heappop(heap)
index += 1
return array