26 Star 369 Fork 112

cyberdash / 数据结构(C++模板实现)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
heap_sort.h 2.45 KB
一键复制 编辑 原始数据 按行查看 历史
Y_Dash 提交于 2023-06-19 19:39 . 排序增加若干doxygen
/*!
* @file heap_sort.h
* @author CyberDash计算机考研, cyberdash@163.com(抖音id:cyberdash_yuan)
* @brief 堆排序
* @version 0.2.1
* @date 2021-09-19
*/
#include "util.h"
/*!
* 大顶堆SiftDown
* @param heap_array 数组
* @param index 执行SiftDown的数组索引
* @param heap_size 堆size
*/
template<typename TElement>
void MaxHeapSiftDown(TElement* heap_array, int index, int heap_size) {
for (int child_index = 2 * index + 1;
child_index < heap_size;
index = child_index, child_index = child_index * 2 + 1)
{
//! index的孩子结点中, 权重较大的结点索引, 赋给child_index
if (child_index < heap_size && heap_array[child_index] < heap_array[child_index + 1]) {
child_index++;
}
//! 如果父节点 >= 子节点, sift down结束
if (heap_array[index] >= heap_array[child_index]) {
break;
}
//! 交换父子结点
Swap(heap_array + index, heap_array + child_index);
}
}
/*!
* 大顶堆SiftUp
* @param heap_array 数组
* @param index 执行SiftUp的数组索引
*/
template<typename TElement>
void MaxHeapSiftUp(TElement* heap_array, int index) {
for (int parent_index = (index - 1) / 2;
parent_index >= 0;
index = parent_index, parent_index = (index - 1) / 2)
{
if (heap_array[parent_index] >= heap_array[index]) {
break;
}
Swap(heap_array + parent_index, heap_array + index);
}
}
/*!
* 构造堆(使用SiftDown)
* @param heap_array 数组
* @param heap_size 数组长度
*/
template<typename TElement>
void BuildHeapBySiftDown(TElement* heap_array, int heap_size) {
int pivot = (heap_size - 2) / 2;
for (int i = pivot; i >= 0; i--) {
MaxHeapSiftDown(heap_array, i, heap_size);
}
}
/*!
* 构造堆(使用SiftUp)
* @param heap_array 数组
* @param heap_size 数组长度
*/
template<typename TElement>
void BuildHeapBySiftUp(TElement* heap_array, int heap_size) {
int pivot = (heap_size - 2) / 2;
for (int i = heap_size - 1; i > pivot; i--) {
MaxHeapSiftUp(heap_array, i);
}
}
/*!
* 堆排序
* @param heap_array 数组
* @param length 数组长度
*/
template<typename TElement>
void HeapSort(TElement* heap_array, int length) {
BuildHeapBySiftDown(heap_array, length);
for (int i = length - 1; i > 0; i--) {
Swap(heap_array, heap_array + i);
MaxHeapSiftDown(heap_array, 0, i - 1);
}
}
C++
1
https://gitee.com/cyberdash/data-structures-cpp.git
git@gitee.com:cyberdash/data-structures-cpp.git
cyberdash
data-structures-cpp
数据结构(C++模板实现)
master

搜索帮助