堆的建立与排序

#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(arr[0]);

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

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

{

swap(&arr[0],&arr[sz-1]);

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

}

}

int main()

{

HeapSort();

}

#include "Heap.h"

void HeapInit(HP* hp)

{

int arr[6] = { 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->arr[hp->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( arr[child] < arr[father] )

{

swap(&arr[child],&arr[father]);

child = father;

}

else

{

break;

}

}

}

//大堆

void Adjustup_max(HPDataType* arr , int child )

{

int father = 0;

while( child )

{

father = (child - 1)/2;

if( arr[child] > arr[father] )

{

swap(&arr[child],&arr[father]);

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->arr[0],&hp->arr[hp->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 && arr[child] < arr[child+1] )//要防止对右孩子解引用

{

child++;

}

if( arr[father] < arr[child] )

{

swap(&arr[father],&arr[child]);

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 && arr[child] > arr[child+1] )//要防止对右孩子解引用

{

child++;

}

if( arr[father] > arr[child] )

{

swap(&arr[father],&arr[child]);

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);

相关推荐
guojl4 分钟前
深度解读jdk8 ConcurrentHashMap设计与源码
java
满分观察网友z6 分钟前
开发者的“右”眼:一个树问题如何拯救我的UI设计(199. 二叉树的右视图)
算法
爱上语文20 分钟前
Redis基础(5):Redis的Java客户端
java·开发语言·数据库·redis·后端
A~taoker26 分钟前
taoker的项目维护(ng服务器)
java·开发语言
HGW68940 分钟前
基于 Elasticsearch 实现地图点聚合
java·elasticsearch·高德地图
hi星尘1 小时前
深度解析:Java内部类与外部类的交互机制
java·开发语言·交互
wuxinyan1231 小时前
Java面试题033:一文深入了解MySQL(5)
java·数据库·mysql·面试
清心歌1 小时前
Java SE线程的创建
java
森焱森1 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机
循环过三天1 小时前
3-1 PID算法改进(积分部分)
笔记·stm32·单片机·学习·算法·pid