C语言堆排序(完整版)

Heap.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>

typedef int HPDataType;

typedef struct Heap
{
	HPDataType* arr;
	int size;//有效数据个数
	int capacity;//容量

}HP;
//堆的初始化
void HPInit(HP* php);

//堆的销毁
void HPDestroy(HP* php);

//堆的尾插
void HPPush(HP* php, HPDataType x);

//堆的尾删
void HPPop(HP* php);

//取头结点
HPDataType HPTop(HP* php);

// 判空
bool HPEmpty(HP* php);

//交换
void swap(int* x, int* y);

//上行调整
AdjustUp(HPDataType* arr, int child);

//下行调整
AdjustDown(HPDataType* arr, int parent, int n);

Heap.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"Heap.h"

//堆的初始化
void HPInit(HP* php)
{
	assert(php);

	php->arr = NULL;
	php->capacity = php->size = 0;
}

//堆的销毁
void HPDestroy(HP* php)
{
	assert(php);
	if (php->arr)
	{
		free(php->arr);
	}
	php->arr = NULL;
	php->capacity = php->size = 0;

}

//交换函数
void swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

//堆上行
AdjustUp(HPDataType* arr, int child)
{
	int parent = (child - 1) / 2;

	while (child > 0)//child == 0 不需要进入循环
	{
		//建大堆, >
		//建小堆, <

		if (arr[child] < arr[parent])
		{
			swap(&arr[parent], &arr[child]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}


}
 
//堆的尾插
void HPPush(HP* php, HPDataType x)
{
	assert(php);

	//判断空间够不够
	if (php->capacity == php->size)
	{
		//先判断空间是否为0
		int newCapacity = php->capacity == 0 ? 4 : 2 * php->capacity;
		HPDataType* newnode = (HPDataType *)realloc(php->arr,newCapacity * sizeof(HP));
		if (newnode == NULL)
		{
			perror("malloc fail!");
			exit(1);
		}
		php->arr = newnode;
		php->capacity = newCapacity;
	}
	php->arr[php->size] = x;//这里要先排序再让size++


	//调整堆
	AdjustUp(php->arr, php->size);


	++php->size;//顺序?
}

//堆下行
AdjustDown(HPDataType* arr, int parent, int n)
{
	int child = parent * 2 + 1;//左孩子
	
	while (child < n)

	{
		//小堆:赵左右孩子中最小的
		//大堆:找左右孩子中最大的

		if (child + 1 < n && arr[child] > arr[child + 1])
		{
			child++;
		}
		if ( arr[child] < arr[parent])
		{
			swap( &arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}


//堆的尾删
void HPPop(HP* php)
{
	assert(php && php->size);

	//arr[0]  arr[size - 1]交换头尾
	swap(&php->arr[0], &php->arr[php->size - 1]);

	--php->size;

	AdjustDown(php->arr, 0, php->size);//顺序?
}

//判空
bool HPEmpty(HP* php)
{
	assert(php);
	return php->size == 0;
}


//返回堆顶
HPDataType HPTop(HP* php)
{
	assert(php && php->size);

	return php->arr[0];
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1


#include<time.h>
#include"Heap.h"

void swap(int* x, int* y);

void test01()
{
	HP hp;
	HPInit(&hp);

	int arr[] = { 17, 20, 10, 13, 19, 15 };
	for (int i = 0; i < 6; i++)
	{
		HPPush(&hp, arr[i]);//将数据插入堆
	}
	while (!HPEmpty(&hp))
	{
		printf("%d ", HPTop(&hp));
		HPPop(&hp);
	}

	HPDestroy(&hp);
}

int main()
{
	test01();
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值