Heap_sort.md


κ°œλ…

그전에, νž™(heap)μžλ£Œκ΅¬μ‘°λŠ”?

Heap

μ™„μ „ μ΄μ§„νŠΈλ¦¬μ˜ ν•˜λ‚˜λ‘œ μš°μ„ μˆœμœ„ 큐λ₯Ό μœ„ν•΄ λ§Œλ“€μ–΄μ§„ 자료ꡬ쑰

  • λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬ μ΅œλŒ€ νž™ ꡬ성

  • μ˜€λ¦„μ°¨μˆœ μ •λ ¬ μ΅œμ†Œ νž™ ꡬ성

νŠΉμ§•

  • 짧은 μ‹œκ°„ λ³΅μž‘λ„

  • μ΅œλŒ“/μ΅œμ†Œκ°’μ΄ ν•„μš”ν•  λ•Œ κ°€μž₯ μœ μš©ν•˜κ²Œ 쓰인닀

μ‹œκ°„ λ³΅μž‘λ„ O(nlogβ‚‚n)

κ΅¬ν˜„

Pythonμ—μ„œλŠ” heapq 라이브러리λ₯Ό μ‚¬μš©ν•˜μ—¬ μ†μ‰½κ²Œ κ΅¬ν˜„μ΄ κ°€λŠ₯ν•˜λ‹€

import heapq

# μ˜€λ¦„μ°¨μˆœ μ •λ ¬
def min_heap(arr):
    return heapq.heapify(arr)

# λ‚΄λ¦Όμ°¨μˆœ μ •λ ¬
def max_heap(arr):
    result = []
    for i in arr:
        heapq.heappush(result, (-i, i)) #μš°μ„ μˆœμœ„, κ°’
    return result[1:] #κ³„μ‚°μ˜ 용이λ₯Ό μœ„ν•΄ 0번쨰 μΈλ±μŠ€λŠ” μ‚¬μš©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

Last updated