惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
宝玉的分享
宝玉的分享
腾讯CDC
博客园_首页
T
Tailwind CSS Blog
月光博客
月光博客
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
SecWiki News
SecWiki News
美团技术团队
P
Privacy International News Feed
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
D
DataBreaches.Net
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
S
Schneier on Security
G
GRAHAM CLULEY
博客园 - 三生石上(FineUI控件)
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
Forbes - Security
Forbes - Security
D
Docker
T
Tenable Blog
S
Secure Thoughts
雷峰网
雷峰网
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Cloudflare Blog
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
阮一峰的网络日志
阮一峰的网络日志

iTimothy

那些让人笑出声的Meme排序算法 再谈费曼学习法 第一性原理读书笔记 我的2025 AWS Certified Developer Associate 认证备考指北 我的 AWS Certified Machine Learning – Specialty 认证之旅 2024年的年终总结:转变与沉淀 迟来的2023年终总结 Modern C++ 学习笔记 -- 左值与右值 告别2022 客制化键盘初体验 RAMA WORKS KARA 聊聊被动收入与躺赚 新加坡驾照转换指北 利用GitHub Actions实现版本自动构建与发布流程 逝去的2021 使用Caddy2托管静态博客 美股期权投资策略学习笔记--期权的基本概念 我的2020 CKAD认证备考经验分享 Emacs启动加速篇
The Heap Sort Algorithm Explained
Timothy · 2023-10-10 · via iTimothy

A heap is a specialized binary tree data structure that satisfies the heap property. In a heap, each node has at most two children, and the key (the value) of each node is greater than or equal to (in a max heap) or less than or equal to (in a min heap) the keys of its children. The binary tree representation of a heap is a complete binary tree, meaning that all levels of the tree are completely filled except possibly for the last level, which is filled from left to right.

Heap Sort

Heap sort is a comparison-based sorting algorithm that uses a max heap to sort elements in ascending order. The algorithm works by first building a max heap from the input array. It then repeatedly extracts the maximum element from the heap and places it at the end of the array. This process is repeated until all elements have been extracted and the array is sorted in ascending order.

Heap sort has a time complexity of O(nlogn) and is an in-place sorting algorithm, meaning it does not require any additional memory beyond the input array.

An array can be used to represent the heap structure by using the following indexing scheme:

  • The root of the heap is at index 0.
  • For any node at index i, its left child is at index 2*i+1 and its right child is at index 2*i+2.

Using this indexing scheme, we can represent a heap as an array and perform heap operations on it efficiently.

For example, to build a max heap from an array, we can start at the last non-leaf node (which is at index n/2-1, where n is the size of the array) and perform the heapify operation on each node from this index down to the root. The heapify operation ensures that the node is the largest of the three by swapping it with the largest child if necessary. By calling heapify on each node from the last non-leaf node down to the root, we ensure that the entire array represents a max heap.

An C++ Example

Here is an C++ example that builds a max heap based on the input vector:

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
void heapify(std::vector<int>& vec, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && vec[left] > vec[largest]) {
largest = left;
}

if (right < n && vec[right] > vec[largest]) {
largest = right;
}

if (largest != i) {
std::swap(vec[i], vec[largest]);
heapify(vec, n, largest);
}
}

void build_max_heap(std::vector<int>& vec) {
int n = vec.size();

for (int i = n / 2 - 1; i >= 0; i--) {
heapify(vec, n, i);
}
}

The build_max_heap function constructs a max heap from a given vector of integers. A max heap is a binary tree where the value of each node is greater than or equal to the values of its children. The build_max_heap function starts by finding the index of the last non-leaf node in the binary tree, which is n/2 - 1, where n is the size of the vector.

It then iterates from this index down to the root of the tree, calling the heapify function on each node.

The heapify function takes a node and its two children and ensures that the node is the largest of the three by swapping it with the largest child if necessary. By calling heapify on each node from the last non-leaf node down to the root, the build_max_heap function ensures that the entire binary tree is a max heap.

Now we call this function and construct a max heap:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
std::vector<int> vec = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};


build_max_heap(vec);


std::cout << "Maximum element: " << vec.front() << std::endl;


std::cout << "The heap: ";
for (auto i : vec) {
std::cout << i << " ";
}

return 0;
}

The output is:

1
2
Maximum element: 9
The heap: 9 6 4 5 5 3 2 1 1 3 5

If we append a new number 12 to the end of the vector and re-construct the max heap:

1
2
3
4
5
6
7
vec.push_back(12);
build_max_heap(vec);

std::cout << "\nThe heap: ";
for (auto i : vec) {
std::cout << i << " ";
}

The output is:

1
The heap: 12 6 9 5 5 4 2 1 1 3 5 3

We will see that, after re-construct the max heap, the newly appended element 12 becomes to the top element of this max heap.

The Heap In C++ Standard Library

std::make_heap and std::priority_queue are both C++ standard library containers that are used to implement heaps. However, they have some differences in their functionality and usage:

  • std::make_heap is a function that takes a range of elements and rearranges them into a heap. It does not provide any additional functionality beyond constructing the heap. Once the heap is constructed, you can use other algorithms like std::sort_heap or std::pop_heap to manipulate the heap.

  • std::priority_queue is a container adapter that provides a priority queue data structure. It is implemented using a heap and provides additional functionality beyond just constructing the heap. It allows you to insert elements into the priority queue, remove the highest-priority element, and access the highest-priority element without removing it. It also provides a way to customize the comparison function used to determine the priority of elements.

215. Kth Largest Element in an Array

347. Top K Frequent Elements

703. Kth Largest Element in a Stream