堆排序算法

堆排序算法

参考博客

// 参见教程:https://www.bilibili.com/video/BV1Eb41147dK
// 堆一种数据结构
/**
 * 1:是完全二叉树,生成顺序满足从上到下,从左到右
 * 2:父节点的值大于子节点
 */

#include <iostream>
#include <ctime>
using namespace std;

// 对某一个节点进行heapify,使其满足父节点的值大于子节点
void heapify(int tree[], int N, int no)
{
    while (no >= 0 && no < N)
    {
        int max = no;
        if (2 * no + 1 < N && tree[2 * no + 1] > tree[max])
            max = 2 * no + 1;
        if (2 * no + 2 < N && tree[2 * no + 2] > tree[max])
            max = 2 * no + 2;
        if (max == no)
        {
            return;
        }
        else
        {
            int temp = tree[max];
            tree[max] = tree[no];
            tree[no] = temp;
            no = max;
        }
    }
}

// 从最后一个父节点开始向上遍历每一个节点,最终生成堆
void buildHeap(int tree[], int N)
{
    if (N <= 1)
        return;
    for (int i = (N - 2) / 2; i >= 0; i--)
    {
        heapify(tree, N, i);
    }
}

// 堆排序
void heapSort(int tree[], int N)
{
    for (int i = N - 1; i >= 0; i--)
    {
        int temp = tree[0];
        tree[0] = tree[i];
        tree[i] = temp;
        heapify(tree, i, 0);
    }
}

int main()
{
    srand((unsigned int)time(NULL));
    int s[10];
    cout << "随机数生成" << endl;
    for (int i = 0; i < 10; i++)
    {
        s[i] = rand() % 10;
        cout << s[i] << "\t";
    }
    cout << endl;
    cout << "堆化" << endl;
    buildHeap(s, 10);
    for (int i = 0; i < 10; i++)
    {
        cout << s[i] << "\t";
    }
    cout << endl;
    cout << "堆排序" << endl;
    heapSort(s, 10);
    for (int i = 0; i < 10; i++)
    {
        cout << s[i] << "\t";
    }
    cout << endl;
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值