Development/CodingTest
최단경로 힙을 이용한 다익스트라 알고리즘 추가 정리...1
Kirok Kim
2022. 2. 15. 23:40
힙 라이브러리 사용
# 최소 힙
import heapq
# 오름차순 힙 정렬(Heap Sort)
def heapsort(iterable):
h=[]
result=[]
# 모든 원소를 차례대로 힘에 삽입
for value in iterable:
heapq.heappush(h, value)
# 힙에 삽입된 모든 원소를 차례대로 꺼내어 담기
for i in range(len(h)):
result.append(heapq.heappop(h))
return result
result = heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
print(result)
실행결과
>>> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# 최대 힙
import heapq
# 내림차순 힙 정렬(Heap Sort)
def heapsort(iterable):
h=[]
result=[]
# 모든 원소를 차례대로 힘에 삽입
for value in iterable:
heapq.heappush(h, -value)
# 힙에 삽입된 모든 원소를 차례대로 꺼내어 담기
for i in range(len(h)):
result.append(-heapq.heappop(h))
return result
result = heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
print(result)
실행결과
>>> [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
반응형