堆的建立与排序

#include "Heap.h"

void HeapSort()

{

int i = 0;

int arr\[\] = { 1,9,8,6,2,3,4,5,7,88,66,99,77 };

int sz = sizeof(arr)/sizeof(arr0);

BuildHeap_max( arr,sz);//建立大堆,采用向下调整

for( ; sz != 1 ; sz--)

{

swap(&arr0,&arrsz-1);

Adjustdown_max(arr , sz-1 , 0 );//将最大值放到堆顶

}

}

int main()

{

HeapSort();

}

#include "Heap.h"

void HeapInit(HP* hp)

{

int arr6 = { 2,3,6,4,5,7 };

hp->size = 6;

hp->capacity = 10;

hp->arr = (HPDataType*)malloc(sizeof(HPDataType)*6);

memcpy(hp->arr,arr,6*sizeof(HPDataType));

//hp->size = 0;

//hp->capacity = 0;

//hp->arr = NULL;

}

void HeapPush(HP* hp,HPDataType x)

{

int newcapacity = 0;

HPDataType* tmp = NULL;

if( hp->size == hp->capacity )

{

newcapacity = hp->capacity == 0 ? 4 : 2 * hp->capacity ;

tmp = (HPDataType*)realloc(hp->arr,sizeof(HPDataType)*newcapacity);

if( tmp == NULL )

{

perror("realloc fail");

exit(1);

}

else

{

hp->arr = tmp;

hp->capacity = newcapacity;

}

}

hp->arrhp-\>size = x;

hp->size++;

Adjustup_min(hp->arr,hp->size-1);

}

//小堆

void Adjustup_min(HPDataType* arr , int child )

{

int father = 0;

while( child )

{

father = (child - 1)/2;

if( arrchild < arrfather )

{

swap(&arrchild,&arrfather);

child = father;

}

else

{

break;

}

}

}

//大堆

void Adjustup_max(HPDataType* arr , int child )

{

int father = 0;

while( child )

{

father = (child - 1)/2;

if( arrchild > arrfather )

{

swap(&arrchild,&arrfather);

child = father;

}

else

{

break;

}

}

}

void swap(HPDataType* a , HPDataType* b )

{

HPDataType temp;

temp = *a;

*a = *b;

*b = temp;

}

void HeapPop(HP* hp)

{

assert(hp);

assert(hp->size);

swap(&hp->arr0,&hp->arrhp-\>size-1);

hp->size--;

Adjustdown_max(hp->arr,hp->size,0);

}

//大堆

void Adjustdown_max(HPDataType* arr , int size , int father)

{

int child = 0;

while( 2 * father + 1 < size )

{

//假设法,假设child为最小的

child = 2 * father + 1;

if( child != size -1 && arrchild < arrchild+1 )//要防止对右孩子解引用

{

child++;

}

if( arrfather < arrchild )

{

swap(&arrfather,&arrchild);

father = child;

}

else

{

break;

}

}

}

void Adjustdown_min(HPDataType* arr , int size , int father)//size是数组的元素个数,father是当前交换的父亲节点

{

int child = 0;

while( 2 * father + 1 < size )

{

//假设法,假设child为最小的

child = 2 * father + 1;

if( child != size -1 && arrchild > arrchild+1 )//要防止对右孩子解引用

{

child++;

}

if( arrfather > arrchild )

{

swap(&arrfather,&arrchild);

father = child;

}

else

{

break;

}

}

}

void BuildHeap_max(HPDataType* arr,int sz)//建立小堆

{

int father = 0;

for( father = (sz - 1 - 1)/2 ; father >= 0 ; father-- )

{

Adjustdown_max(arr , sz , father );

}

}

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <assert.h>

typedef int HPDataType;

typedef struct Heap

{

HPDataType* arr;

int size;

int capacity;

}HP;

void HeapInit(HP* hp);

void HeapPush(HP* hp,HPDataType x);

void HeapPop(HP* hp);

void swap(HPDataType* a , HPDataType* b );

void Adjustup_max(HPDataType* arr , int child );

void Adjustup_min(HPDataType* arr , int child );

void Adjustdown_min(HPDataType* arr , int size , int father);

void Adjustdown_max(HPDataType* arr , int size , int father);

void BuildHeap_min(HPDataType* arr,int sz);

void BuildHeap_max(HPDataType* arr,int sz);

相关推荐
xcl09254 小时前
幼儿托育系统开发实战:从需求分析到上线全流程指南
java·大数据·需求分析
吴声子夜歌4 小时前
ApacheCommons——commons-cli(命令行参数解析)
java·开发语言·apache
小小猪的春天4 小时前
Java 手写第一个 MCP Server:Spring AI MCP 半小时跑通
java·人工智能·spring boot·ai编程
find1star4 小时前
LeetCode 141:环形链表
java·算法·leetcode·链表
迅翼SwiftWing4 小时前
编队系列(总)|固定翼编队如何实现稳定飞行?通信架构、协同算法与工程约束深度解析
算法·架构
LB21124 小时前
力扣160 21 86
算法·leetcode·职场和发展
今天AI了吗5 小时前
DeepSeek Harness 深度解析:从评测架构到实战落地
java·网络·数据库·人工智能·架构·java-ee
许彰午5 小时前
27-AuthService登录链路
java·低代码·架构
泡海椒5 小时前
JQuick-Curl 性能分析:并发场景下的性能表现与调优,第三方接口调用不只要快写也要稳跑
java·开发语言·okhttp
小智老师PMP5 小时前
2026深度解析|PMP第八版与NPDP核心侧重点本质区别(管理类证书怎么选)
开发语言·分布式·算法·职场和发展·产品经理