■ heapify/heappop 함수를 사용해 리스트를 힙 정렬하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import heapq sourceList = [14, 10, 6, 8, 12, 4] heapq.heapify(sourceList) print(sourceList) # [4, 8, 6, 10, 12, 14] targetLst = [] while sourceList: targetLst.append(heapq.heappop(sourceList)) print(targetLst) # [4, 6, 8, 10, 12, 14] |