文章目录
- 前言
 - 🌟一、建堆的两种方式:
 - 🌏1.1 向上调整建堆(堆排序):
 - 💫1.1.1 完整代码:
 - 💫1.1.2 流程图(以小堆为例):升序:建大堆
 - 💫1.1.3 流程图(以小堆为例):降序:建小堆
 
- 🌏1.2 向下调整建堆(堆排序):
 - 💫1.2.1 完整代码:
 - 💫1.2.2 流程图:
 
- 🌟二、两种建堆方式时间复杂度比较:
 - 🌏2.1 向上调整建堆:
 - 🌏2.2 向下调整建堆:
 
- 🌟三、堆排序的时间复杂度:O(N*logN)
 - 🌟四、呼应一下上章节的部分:利用堆使数据有序(不建议)
 - 😽总结
 
前言
👧个人主页:@小沈熬夜秃头中୧⍤⃝❅
😚小编介绍:欢迎来到我的乱七八糟小星球🌝
📋专栏:数据结构
🔑本章内容:堆排序—堆应用(一)
送给各位💌:日落跌入昭昭星野 人间忽晚 山河以秋
记得 评论📝 +点赞👍 +收藏😽 +关注💞哦~
提示:以下是本篇文章正文内容,下面案例可供参考
🌟一、建堆的两种方式:
🌏1.1 向上调整建堆(堆排序):
💫1.1.1 完整代码:
//Heap.h
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int HPDataType;
typedef struct Heap
{
	HPDataType* a;
	int size;
	int capacity;
}HP;
void AdjustDown(int* a, int n, int parent);
//因为要在Test.c中调用这个函数而Test.c中包含#include"Heap.h"所以要在这里包含一下
void AdjustUp(HPDataType* a, int child);
void HeapInit(HP* php);
void HeapDestroy(HP* php);
void HeapPush(HP* php, HPDataType x);
void HeapPop(HP* php);
HPDataType HeapTop(HP* php);
bool HeapEmpty(HP* php);
int HeapSize(HP* php);
//Heap.c
void HeapInit(HP* php)
{
	assert(php);
	php->a = NULL;
	php->size = 0;
	php->capacity = 0;
}
void Swap(HPDataType* p1, HPDataType* p2)
{
	HPDataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void AdjustUp(HPDataType* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] < a[parent])
		{
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
void AdjustDown(int* a, int n, int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] < a[child])
		{
			child++;
		}
		if (a[child] < a[parent])
		{
			Swap(&a[parent], &a[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
//Test.c
void HeapSort(int* a, int n)
{
	HP hp;
	HeapInit(&hp);
	for (int i = 1;i<n; i++)
	{
	//堆向上调整算法
		AdjustUp(a, i);//当i=0时,也就是孩子下标是0此时一个数据可以看作一个堆所以从1开始向上调整
	}
	int end = n - 1;
	while (end > 0)
	{
		//小堆为例通过建堆第一个肯定是最小数
		Swap(&a[0], &a[end]);
		//调整选出次小数
		AdjustDown(a, end, 0);//这里的end是n-1是最后一个数据的下标也是除了最后一个数据外前面数据的个数,所以可以传end过去
		end--;
	}
}
int main()
{
	//HPTest();
	int a[] = { 7,8,3,5,1,9,5,4 };
	HeapSort(a, sizeof(a) / sizeof(a[0]));
	return 0;
}
 
💫1.1.2 流程图(以小堆为例):升序:建大堆
最后得到的就是一个升序
💫1.1.3 流程图(以小堆为例):降序:建小堆
最后得到的就是一个降序
🌏1.2 向下调整建堆(堆排序):
💫1.2.1 完整代码:
//Heap.h
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int HPDataType;
typedef struct Heap
{
	HPDataType* a;
	int size;
	int capacity;
}HP;
void AdjustDown(int* a, int n, int parent);
void AdjustUp(HPDataType* a, int child);
void HeapInit(HP* php);
void HeapDestroy(HP* php);
void HeapPush(HP* php, HPDataType x);
void HeapPop(HP* php);
HPDataType HeapTop(HP* php);
bool HeapEmpty(HP* php);
int HeapSize(HP* php);
//Heap.c
void HeapInit(HP* php)
{
	assert(php);
	php->a = NULL;
	php->size = 0;
	php->capacity = 0;
}
void Swap(HPDataType* p1, HPDataType* p2)
{
	HPDataType tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
void AdjustUp(HPDataType* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] < a[parent])
		{
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}
void AdjustDown(int* a, int n, int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] < a[child])
		{
			child++;
		}
		if (a[child] < a[parent])
		{
			Swap(&a[parent], &a[child]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
//Test.c
#include"Heap.h"
//直接建堆来调整---向下调整建堆
void HeapSort(int* a, int n)
{
	HP hp;
	HeapInit(&hp);
	for (int i = (n-1-1)/2; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		end--;
	}
}
int main()
{
	//HPTest();
	int a[] = { 7,8,3,5,1,9,5,4 };
	HeapSort(a, sizeof(a) / sizeof(a[0]));
	return 0;
}
 
💫1.2.2 流程图:
🌟二、两种建堆方式时间复杂度比较:
通过对两种建堆方式的比较更建议使用向下调整建堆但是向上调整建堆更容易理解看个人情况
🌏2.1 向上调整建堆:
🌏2.2 向下调整建堆:
🌟三、堆排序的时间复杂度:O(N*logN)
🌟四、呼应一下上章节的部分:利用堆使数据有序(不建议)
利用创建的堆数组排序:我们可以采用下面这种方法 — 先建堆(NlogN)太麻烦,然后来回拷贝数据(空间复杂度高)—具体代码可以看【数据结构】— 博主拍了拍你并向你扔了一“堆”二叉树(堆的概念+结构+代码实现)其中只有Test.c文件中做了以下修改其余没变
void HeapSort(int* a,int n)
{
		HP hp;
		HeapInit(&hp);
		//建堆N*logN
		for (int i = 0;i<n; i++)
		{
			HeapPush(&hp, a[i]);
		}
		//N*logN
		int i = 0;
		while (!HeapEmpty(&hp))
		{
			int top = HeapTop(&hp);
			a[i++] = top;
			HeapPop(&hp);
		}
		for (int i = 0; i < n; i++)
		{
			printf("%d ", a[i]);
		}
		HeapDestroy(&hp);
}
int main()
{
	int a[] = { 7,8,3,5,1,9,5,4 };
	HeapSort(a, sizeof(a) / sizeof(a[0]));
	return 0;
}
 
😽总结
😽Ending,今天的二叉树 — 堆应用一(堆排序) 的内容就到此结束啦~,如果后续想了解更多,就请关注我吧,一键三连哦 ~


























