C-log

심심풀이 땅콩🥜 : 힙(Heap) 본문

카테고리 없음

심심풀이 땅콩🥜 : 힙(Heap)

4:Bee 2023. 12. 2. 23:26
728x90
Name Algorithm type
Heap 완전 이진 트리 > 우선 순위 큐

 

 

[자료구조] 힙(heap)이란 - Heee's Development Blog

Step by step goes a long way.

gmlwjd9405.github.io

 

 

08. 파이썬으로 힙(heap) 구현하기

이미 공부한 것처럼 큐(Queue)는 자료가 들어온 순서대로 나가는 선입선출(First In First Out)의 자료 구조다. 들어가는 자료에 우선순위를 매겨서 들어간 순서와 …

wikidocs.net

 

 

[Python] 힙 자료구조 / 힙큐(heapq) / 파이썬에서 heapq 모듈 사용하기

힙은 특정한 규칙을 가지는 트리로, 최댓값과 최솟값을 찾는 연산을 빠르게 하기 위해 고안된 완전이진트리를 기본으로 한다. 힙 property : A가 B의 부모노드이면 A의 키값과 B의 키값 사이에는 대

littlefoxdiary.tistory.com

힙 알고리즘은 큐(Queue) 알고리즘을 알아야한다. 아래 코드를 보자

import heapq
# heapify


def heap_sort(arr):
    heap = []
    for element in arr:
        heapq.heappush(heap, element)
        print(heap, end=" ")
    print(heap)
    sorted_arr = []
    while heap:
        sorted_arr.append(heapq.heappop(heap))

    return sorted_arr


arr = [5, 3, 8, 4, 2]
sorted_arr = heap_sort(arr)
print(sorted_arr)

이외의 heapify라는 함수도 있다. 나중에 문제를 풀어볼 일이 있을 때 한번 풀어보자.

728x90
Comments