栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构 堆排序

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构 堆排序

堆与堆排序 【对下面视频的总结】https://blog.csdn.net/Appleeatingboy/article/details/119607212?spm=1001.2014.3001.5501

堆排序 【代码有问题?】https://www.bilibili.com/video/BV1Eb41147dK?spm_id_from=333.999.0.0

#include 
using namespace std;

void Heapfi(int heap[],int i,int size){
    int c1 = 2*i+1;
    int c2 = 2*i+2;
    int max = i;
    if(c1 < size && heap[c1] > heap[max])
        max = c1;
    if(c2 < size && heap[c2] > heap[max])
        max = c2;
    if(max != i){
        swap(heap[max],heap[i]);
        Heapfi(heap,max,size);
    }
}

void HeapBuild(int heap[],int size){
    for(int i=(size-2)/2;i>=0;i--){
        Heapfi(heap,i,size);
    }
}

void HeapSort(int heap[],int size){
    HeapBuild(heap,size);
    for(int i=size-1;i>=0;i--){
        swap(heap[i],heap[0]);
        Heapfi(heap,0,i);
    }
}

int main(){
    int array[] = {7,35,15,10,30};

    HeapSort(array,5);
    for(int i=0;i<5;i++){
        cout<

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/715274.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号