堆排序算法详解及实现-----------c语言

堆排序原理:
  堆排序指的是将大堆(小堆)堆顶(即下标为0)元素与堆的最后一个(即下标为hp->size - 1)元素交换,hp->size–,将其余的元素再次调整成大堆(小堆),再次将堆顶(即下标为0)元素与堆的最后一个(即下标为hp->size - 1)元素交换,hp->size–,将其余的元素再次调整成大堆(小堆)…………重复上述步骤,直至hp->size<1,将hp->size恢复为原来的值,循环不在继续。
关于升序或者降序应该建立什么堆???
排升序:
  若想排升序即最大值应该在最后面,根据堆排序原理,应该建大堆,这样将堆顶元素与最后一个元素交换之后,最大值换到了最后面。
排降序:
  若想排降序即最小值应该在最后面,根据堆排序原理,应该建小堆,这样将堆顶元素与最后一个元素交换之后,最小值换到了最后面。
堆的详细概念以及创建等操作
**代码实现: **

#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>

typedef struct Heap
{
	int *data;
	int size;  
}Heap;
//堆排序
void Heap_Sort(int *array, int size);
//
void swap(int *x, int *y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}
void AdjustHeap(Heap *hp, int parent)
{
	int child = 2 * parent + 1;
	while (child < hp->size)
	{
		//找出左右孩子的较大值
		if (child + 1 < hp->size&&hp->data[child + 1] > hp->data[child])
		{
			child = child + 1;
		}
		//如果孩子中的最大值比父母大,则交换
		if (hp->data[child]>hp->data[parent])
		{
			swap(&hp->data[child], &hp->data[parent]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
			return;
	}
}
//创建堆
void CreatHeap(Heap *hp, int *array, int size)
{
	int root = 0;
	hp->data = (int *)malloc(size*sizeof(int));
	if (hp->data == NULL)
	{
		return;
	}
	//先将数组中的元素拷贝至堆中
	memcpy(hp->data, array, size*sizeof(int));
	//向下调整堆
	hp->size = size;

	root = (hp->size - 2) / 2;
	for (; root >= 0; root--)
	{
		AdjustHeap(hp, root);
	}
}
//排序
void HeapSort(Heap *hp)
{
	int size = hp->size;
	while (hp->size > 1)
	{
		swap(&hp->data[0], &hp->data[hp->size - 1]);
		hp->size--;
		AdjustHeap(hp, 0);
	}
	hp->size = size;
}
//堆排序
void Heap_Sort(int *array, int size)
{
	Heap hp;
	//创建堆
	CreatHeap(&hp, array, size);
	//排序
	HeapSort(&hp);
	memcpy(array,hp.data, size*sizeof(int));
}

测试文件:

#include"sort.h"
void TestHeapSort()
{
	int array[] = { 10, 2, 5, 4, 6, 9, 3, 1, 0 };
	printf("排序前:");
	//打印
	Print(array, sizeof(array) / sizeof(array[0]));
	//堆排序
	Heap_Sort(array, sizeof(array) / sizeof(array[0]));
	printf("排序后:");
	//打印
	Print(array, sizeof(array) / sizeof(array[0]));
}
int main()
{
	TestHeapSort();
	system("pause");
	return 0;
}

在这里插入图片描述
时间复杂度: O(nlogn)
空间复杂度: O(1)
稳定性: 不稳定

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值