3.1 Sorting - 3.1.4 Heap Sort

2024. 1. 15. 21:23ㆍCS - Roadmap.sh/3. Common Algorithms

CS - 3. Common Algorithms - 3.1. Sorting - 3.1.4 Heap Sort

 

πŸ’‘Heap sort is a comparison based sorting algorithm. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for remaining element.

νž™ μ •λ ¬μ€ λΉ„ꡐ κΈ°λ°˜ μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μž…λ‹ˆλ‹€. λ¨Όμ € μ΅œλŒ€ μš”μ†Œλ₯Ό μ°Ύμ€ λ‹€μŒ μ΅œλŒ€ μš”μ†Œλ₯Ό λ§ˆμ§€λ§‰μ— λ°°μΉ˜ν•˜λŠ” μ„ νƒ μ •λ ¬κ³Ό μœ μ‚¬ν•©λ‹ˆλ‹€. λ‚˜λ¨Έμ§€ μš”μ†Œμ— λŒ€ν•΄μ„œλ„ λ™μΌν•œ κ³Όμ •μ„ λ°˜λ³΅ν•©λ‹ˆλ‹€.

 

μ•žμ—μ„œ λ°μ΄ν„°μ˜ 양이 λ§Žκ±°λ‚˜ λ¬΄μž‘μœ„λ‘œ λ°°μ—΄λœ 데이터일 경우 퀡, νž™ 같은 κ³ κΈ‰ μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ„ μ‚¬μš©ν•˜λŠ” 것이 μ’‹λ‹€κ³  λ§ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

κ·Έλž˜μ„œ μ–Όλ§ˆλ‚˜ 쒋은지 μ•Œμ•„λ΄…μ‹œλ‹€.

 

νž™ μ •λ ¬ (Heap Sort)은 νž™μ΄λΌλŠ” νŠΉλ³„ν•œ 이진 트리 자료ꡬ쑰λ₯Ό μ‚¬μš©ν•˜λŠ” μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜ μž…λ‹ˆλ‹€.

νž™μ€ https://parkpakrsu.tistory.com/331

 

Data structures (with python) - 3. Heap

Data structures (with python) - 3. Heap πŸ’‘ Heap is a tree-based data structure that follows the properties of a complete binary tree and is either a Min Heap or a Max Heap. νž™μ€ μ™„μ „ν•œ 이진 트리의 속성을 λ”°λ₯΄λŠ” 트리 기반 데이터 ꡬ

parkpakrsu.tistory.com

λ‹€λ€˜μŠ΅λ‹ˆλ‹€.

 

ν•œμ€„λ‘œ μš”μ•½ν•œλ‹€λ©΄

'μ΅œλŒ“κ°’ λ˜λŠ” μ΅œμ†Ÿκ°’μ„ λΉ λ₯΄κ²Œ μ°ΎκΈ° μœ„ν•΄ μ‚¬μš©λ˜λŠ” λΆ€λͺ¨-μžμ‹ λ…Έλ“œκ°„μ˜ 관계λ₯Ό λ°”νƒ•μœΌλ‘œ μ •λ ¬λ˜λŠ” μ™„μ „ 이진 트리.'

라고 ν• μˆ˜ μžˆκ² λ‹€.

λΆ€λͺ¨ λ…Έλ“œμ˜ 값이 μžμ‹λ…Έλ“œμ˜ 값보닀 항상 ν¬κ±°λ‚˜ μž‘μ€νŠΉμ„±μ„ 가지고 μžˆλ‹€.

이런 νŠΉμ„± λ•Œλ¬Έμ— νž™μ€ νλ‚˜ 정렬을 ν•˜λŠ”λ° κ°•λ ₯ν•œ μ„±λŠ₯을 λ°œνœ˜ν•œλ‹€.

 

νž™ μ •λ ¬(Heap Sort)λŠ” μ΄λŸ¬ν•œ νž™μ˜ νŠΉμ„±μ„ μ΄μš©ν•©λ‹ˆλ‹€.

λ¨Όμ € 배열을 μ΅œλŒ€ νž™μ˜ ν˜•νƒœλ‘œ λ³€ν™˜ν•œ λ’€, νž™μœΌ γ…£λ£¨νŠΈμ— 항상 μ΅œλŒ€κ°’μ΄ μœ„μΉ˜ν•˜κ²Œλ” λ§Œλ“­λ‹ˆλ‹€.

그리고 이 μ΅œλŒ€κ°’μ„ νž™μ˜ λ§ˆμ§€λ§‰ λ…Έλ“œμ™€ κ΅ν™˜ν•˜κ³  νž™ 크기λ₯Ό ν•˜λ‚˜ 쀄여 정렬을 μ§„ν–‰ν•©λ‹ˆλ‹€.

 

νž™ μ •λ ¬μ˜ λ™μž‘ 과정은 μ΄λ ‡μŠ΅λ‹ˆλ‹€.

1. μž…λ ₯ 배열을 μ΅œλŒ€ νž™μœΌλ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€.

2. νž™μ˜ 루트(μ΅œλŒ€κ°’)을 νž™μ˜ λ§ˆμ§€λ§‰ μš”μ†Œμ™€ κ΅ν™˜ν•©λ‹ˆλ‹€.

3. νž™ 크기λ₯Ό 1 μ€„μž…λ‹ˆλ‹€.(즉, λ§ˆμ§€λ§‰ μš”μ†Œλ₯Ό νž™μ—μ„œ μ œκ±°ν•©λ‹ˆλ‹€.)

4. μƒˆλ‘œμš΄ λ£¨νŠΈμ— λŒ€ν•΄ νž™μ„ μž¬μ‘°μ •ν•©λ‹ˆλ‹€.

5. μœ„μ˜ 과정을 νž™μ˜ 크기가 1이 될 λ–„κΉŒμ§€ λ°˜λ³΅ν•©λ‹ˆλ‹€.

 

 

In Python
import heapq

def heap_sort(arr):
    heapq.heapify(arr)
    return [heapq.heappop(arr) for _ in range(len(arr))]

νŒŒμ΄μ¬μ—μ„œλŠ” heapq λͺ¨λ“ˆμ„ 톡해 κ΅¬ν˜„ν• μˆ˜ μžˆμŠ΅λ‹ˆλ‹€.

 

쒀더 ꡬ체적인 μ˜ˆμ‹œλ₯Ό λ“€κ² μŠ΅λ‹ˆλ‹€.

# 1. νž™μƒμ„±
import heapq
numbers = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
heapq.heapify(numbers)

# 2. μ›μ†Œ μΆ”κ°€ :heappush O(log n)
heapq.heappush(numbers, -5)

# 3. μ›μ†Œ 제거 :heappop O(log n)
smallest = heapq.heappop(numbers)

# 4. μ΅œλŒ€/μ΅œμ†Œκ°’ 쑰회 : 파이썬의 νž™μ€ μ΅œμ†Œ νž™(min heap)μ΄λ―€λ‘œ, κ°€μž₯ μž‘μ€ 값은 항상 인덱슀 0, 즉 νž™μ˜ λ£¨νŠΈμ— μœ„μΉ˜ν•©λ‹ˆλ‹€.
# λ”°λΌμ„œ O(1)의 μ‹œκ°„ λ³΅μž‘λ„λ‘œ μ΅œμ†Œκ°’μ„ μ‘°νšŒν•  수 μžˆμŠ΅λ‹ˆλ‹€.
smallest = numbers[0]

# 5. μš°μ„ μˆœμœ„ 큐 μˆ˜ν˜„ : νŠœν”Œμ˜ 리슀트λ₯Ό νž™μœΌλ‘œ λ§Œλ“€λ©΄ 첫 번쨰 μ›μ†Œλ₯Ό κΈ°μ€€μœΌλ‘œ μ΅œμ†Œ νž™μ΄ κ΅¬μ„±λ©λ‹ˆλ‹€.
# 이λ₯Ό μ΄μš©ν•˜λ©΄ μš°μ„ μˆœμœ„μ™€ 값이 쌍으둜 이루어진 데이터λ₯Ό κ΄€λ¦¬ν•˜λŠ”λ° μœ μš©ν•©λ‹ˆλ‹€.
priority_queue = []
heapq.heappush(priority_queue, (2, 'code'))
heapq.heappush(priority_queue, (1, 'eat'))
heapq.heappush(priority_queue, (3, 'sleep'))
while priority_queue:
    print(heapq.heappop(priority_queue)[1])  # 'eat', 'code', 'sleep' μˆœμ„œλ‘œ 좜λ ₯

 

 

정리

 

νž™ μ •λ ¬μ˜ μ‹œκ°„ λ³΅μž‘λ„λŠ” O(log n)으둜, λŒ€λΆ€λΆ„μ˜ κ²½μš°μ—μ„œ 효율적으둜 μž‘λ™ν•©λ‹ˆλ‹€.

νž™ 정렬은 λ°μ΄ν„°μ˜ μž…λ ₯ μƒνƒœμ— 관계없이 μΌμ •ν•œ μ‹€ν–‰ μ‹œκ°„μ„ κ°€μ§€λŠ” 것이 νŠΉμ§•μž…λ‹ˆλ‹€.

즉, 이미 μ •λ ¬λœ λ°μ΄ν„°λ‚˜ μ—­μˆœμœΌλ‘œ μ •λ ¬λœ 데이터에ㅔ λŒ€ν•΄μ„œλ„ μ‹œκ°„ λ³΅μž‘λ„λŠ” λ³€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

λ”°λΌμ„œ, μ •λ ¬λ˜μ§€ μ•Šμ€ 데이터λ₯Ό μ •λ ¬ν•΄μ•Ό ν•˜λŠ” κ²½μš°λ‚˜ μ΅œμ•…μ˜ 경우의 μ‹œκ°„ λ³΅μž‘λ„κ°€ μ€‘μš”ν•œ 경우 μœ μš©ν•˜κ²Œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

728x90

'CS - Roadmap.sh > 3. Common Algorithms' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

3.1. Sorting - 3.1.6 Merge Sort  (0) 2024.01.20
3.1 Sorting - 3.1.5 Quick Sort  (0) 2024.01.16
3.1. Sorting - 3.1.3 Insertion Sort  (0) 2024.01.15
3.1. Sorting - 3.1.2 Selection Sort  (1) 2024.01.14
3.1. Sorting - 3.1.1 Bubble sort  (1) 2024.01.13