【经典排序算法】堆排序(精简版)

什么是堆排序:

堆排序(Heapsort)是指利用堆(完全二叉树)这种数据结构所设计的一种排序算法,它是选择排序的一种。需要注意的是排升序要建大堆,排降序建小堆。
 
堆排序排序的特性总结:
1. 堆排序使用堆来选数,效率就高了很多。
2. 时间复杂度:O(N*logN)
3. 空间复杂度:O(1)
4. 稳定性:不稳定
 
在开始堆排序之前有两个重要的概念:
大堆:父节点>=子节点的堆。
小堆:父节点<=子节点的堆。
堆是什么:堆是完全二叉树。
 
在逻辑结构方面我们可以将一个数组想象成一个堆。
在物理结构方面看堆其实是一个数组。
如下:
 
9e7a542e352d48298130264c153196ef.png

 

在我看来,堆排序中最重要的是向上调整和向下调整算法。

堆排序是基于这两个算法来进行的。

向上调整算法:

目的:
 当向堆中插入新元素时,为了维护堆的性质,需要对该元素进行向上调整。向上调整法就是从新插入的节点开始,通过与其父节点的比较和交换,确保该节点的值不大于(对于大根堆)或不小于(对于小根堆)其父节点的值。

//以大堆为例
void Adjustup(int* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] > a[parent])
			swap(a[child], a[parent]);
		else
			break;
		child = parent;
		parent = (child - 1) / 2;
	}
}

向下调整算法:

目的:
 当向堆中插入新元素时,为了维护堆的性质,需要对该元素进行向上调整。向上调整法就是从新插入的节点开始,通过与其父节点的比较和交换,确保该节点的值不大于(对于大根堆)或不小于(对于小根堆)其父节点的值。

//向下调整算法
//大堆为例
void Adjustdown(int* a, int parent, int n)//n是数组a的元素个数
{
	int child = 2 * parent + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child] < a[child + 1])
		{
			++child;
		}
		if (a[child] > a[parent])
		{
			swap(a[child], a[parent]);
		}
		else
			break;
		parent = child;
		child = parent * 2 + 1;
	}
}

需要注意的是对堆进行向上向下调整算法是要确保堆左子树和右子树是大堆/小堆。

下面我们来实现一下堆排序的完整代码:

void swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}
//以大堆为例
void Adjustup(int* a, int child)
{
	int parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] > a[parent])
			swap(a[child], a[parent]);
		else
			break;
		child = parent;
		parent = (child - 1) / 2;
	}
}
//向下调整算法
//大堆为例
void Adjustdown(int* a, int parent, int n)//n是数组a的元素个数
{
	int child = 2 * parent + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child] < a[child + 1])
		{
			++child;
		}
		if (a[child] > a[parent])
		{
			swap(a[child], a[parent]);
		}
		else
			break;
		parent = child;
		child = parent * 2 + 1;
	}
}
void HeapSort(int* a, int n)
{
	//首先利用向上调整算法建一个大堆(即父节点>=子节点的完全二叉树二叉树)
	for (int i = 1;i < n;i++)
	{
		Adjustup(a, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		swap(a[0], a[end]);
		Adjustdown(a, 0, end);
        --end;
	}
}

上述堆排序代码是针对数组升序编写的,这里就不写降序的代码了,降序的堆排序和升序的如出一辙。

试验结果如下:

#include <iostream>
using namespace std;
int main()
{
	int arr[] = { 77,99,2,1,2,8,6,7,0 };
	HeapSort(arr, sizeof(arr) / sizeof(arr[0]));
	for (auto e : arr)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

bd2d14aa4ef64e6ca2c65a66eea7acd9.png

 

 
 

 

相关推荐

  1. 排序算法-排序

    2024-06-08 05:10:02       18 阅读
  2. [排序算法]排序

    2024-06-08 05:10:02       27 阅读
  3. 排序算法】之排序

    2024-06-08 05:10:02       45 阅读
  4. 九、算法-排序-排序

    2024-06-08 05:10:02       18 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-08 05:10:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-08 05:10:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-08 05:10:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-08 05:10:02       18 阅读

热门阅读

  1. Always语句和assign的用法

    2024-06-08 05:10:02       9 阅读
  2. spring-boot 2.7.18整合sharding-jdbc-spring-boot-starter 4.1.1

    2024-06-08 05:10:02       12 阅读
  3. 进位(bit)

    2024-06-08 05:10:02       9 阅读
  4. 如何在Python中创建和使用自定义模块

    2024-06-08 05:10:02       11 阅读
  5. 局域网、城域网、广域网的ip

    2024-06-08 05:10:02       10 阅读
  6. SpringMVC:@RequestMapping注解

    2024-06-08 05:10:02       9 阅读
  7. 【嵌入式 - 关于MCU的内存分配】

    2024-06-08 05:10:02       9 阅读