| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
- function
- string
- Python
- C++
- const
- Class
- 알고리즘
- 문자열
- 배열
- vscode
- raw data
- Pre-processing
- 반복문
- Object Oriented Programming
- 함수
- 포인터
- OOP
- 백준
- baekjoon
- 파이썬
- 티스토리챌린지
- assignment operator
- pass by reference
- programming
- Data Science
- array
- Deep Learning
- 오블완
- pointer
- predictive analysis
- Today
- Total
Channi Studies
[Data Structure] Heap Sort 본문
Heapify Operation
Heapsort is a sorting algorithm that takes advantage of a max-heap's properties by repeatedly removing the max and building a sorted array in reverse order. An array of unsorted values must first be converted into a heap. The heapify opeartion is used to turn an array into a heap. Since leaf nodes already satisfy the max heap property, heapifying to build a max-heap is achieved by percolating down on every non-leaf node in reverse order.

The heapify operation starts on the internal node with the largest index and continudes down to, and including, the root node at index 0.
Given a binary tree with N nodes, the largest internal node index is ⌊N/2⌋ - 1.

Heap Sort
Heapsort uses 2 loops to sort an array. The first loop heapifies the array using MaxHeapPercolateDown. The second loop removes the maximum value, stores that value at the end index, and decrements the end index, until the end index is 0.
# Binary max heap percolate down
def max_heap_percolate_down(node_index, heap_list, list_size):
child_index = 2 * node_index + 1
value = heap_list[node_index]
while child_index < list_size:
# Find the max among the node and all the node's children
max_value = value
max_index = -1
i = 0
while i < 2 and i + child_index < list_size:
if heap_list[i + child_index] > max_value:
max_value = heap_list[i + child_index]
max_index = i + child_index
i = i + 1
if max_value == value:
return
# Swap heap_list[node_index] and heap_list[max_index]
temp = heap_list[node_index]
heap_list[node_index] = heap_list[max_index]
heap_list[max_index] = temp
node_index = max_index
child_index = 2 * node_index + 1
# Sorts the list of numbers using the heap sort algorithm
def heap_sort(numbers):
# Heapify numbers list
i = len(numbers) // 2 - 1
while i >= 0:
max_heap_percolate_down(i, numbers, len(numbers))
i = i - 1
i = len(numbers) - 1
while i > 0:
# Swap numbers[0] and numbers[i]
temp = numbers[0]
numbers[0] = numbers[i]
numbers[i] = temp
max_heap_percolate_down(0, numbers, i)
i = i - 1'Data Science > Data Structure & Algorithm' 카테고리의 다른 글
| [ADT] Priority Queue Abstract Data Type (0) | 2025.05.09 |
|---|---|
| Abstract Data Type (ADT) V.S. Data Structure | 추상 자료형 vs 자료 구조형 (0) | 2025.05.09 |
| [Data Structure] Heaps Python Implementation (0) | 2025.05.08 |
| [Data Structure] Heaps - 2 | Heaps Using Arrays (0) | 2025.05.08 |
| [Data Structure] Heaps - 1 | Max-heap (최대 힙) and Min-heap (최소 힙) (0) | 2025.05.08 |