堆的应用二:堆排序
前言
`
堆的应用(堆排序)会涉及到大量的堆的知识:如果有不理解的可以参考 : 堆的模拟实现
一、题目
用堆来实现排升序和降序(分别建大堆和小堆)
题目:把 20 17 4 16 5 3 这些数字排成升序!要求:时间复杂度优化到 : O(1)
二、思路+图解
1.思路一:
代码如下(示例):
void HeapSort(int* a, int n)
{
HP hp;
HeapInit(&hp);
// 建立一个N个小堆
for (int i = 0; i < n; ++i)
{
HeapPush(&hp, a[i]);
}
// Pop N 次
for (int i = 0; i < n; ++i)
{
a[i] = HeapTop(&hp);
HeapPop(&hp);
}
HeapDestroy(&hp);
}
2.思路二:
为了方便画图,思路二我们排序 :20 17 4 16
三、源代码
代码如下(示例):
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int HPDataType;
typedef struct Heap
{
HPDataType* a;
int size;
int capacity;
}HP;
void AdjustUp(int* a, int child);//向上调整
void AdjustDown(int* a, int n, int parent);//向下调整
void Swap(HPDataType* px, HPDataType* py);//交换
void HeapInit(HP* hp);//初始化堆
void HeapDestroy(HP* hp);//销毁堆
void HeapPush(HP* hp, HPDataType x);//尾插数据
void HeapPop(HP* hp);//删除堆顶的数据
HPDataType HeapTop(HP* hp);//取堆顶的数据
void HeapPrint(HP* hp);//打印堆(数组)
bool HeapEmpty(HP* hp);//判断堆是否为空
int HeapSize(HP* hp);//堆结点个数
void Swap(HPDataType * px, HPDataType * py)
{
HPDataType tmp = *px;
*px = *py;
*py = tmp;
}
void HeapInit(HP* hp)
{
assert(hp);
hp->a = NULL;
hp->size = hp->capacity = 0;
}
void HeapDestroy(HP* hp)
{
assert(hp);
free(hp->a);
hp->capacity = hp->size = 0;
}
void AdjustUp(int* a, int child)
{
assert(a);
int parent = (child - 1) / 2;
//while (parent >= 0)
while (child > 0)
{
if (a[child] > a[parent])
{
Swap(&a[child], &a[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void HeapPrint(HP* hp)
{
for (int i = 0; i < hp->size; ++i)
{
printf("%d ", hp->a[i]);
}
printf("\n");
}
void HeapPush(HP* hp, HPDataType x)
{
assert(hp);
if (hp->size == hp->capacity)
{
size_t newCapacity = hp->capacity == 0 ? 4 : hp->capacity * 2;
HPDataType* tmp = realloc(hp->a, sizeof(HPDataType) * newCapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
hp->a = tmp;
hp->capacity = newCapacity;
}
hp->a[hp->size] = x;
hp->size++;
AdjustUp(hp->a, hp->size - 1);
}
bool HeapEmpty(HP* hp)
{
assert(hp);
return hp->size == 0;
}
int HeapSize(HP* hp)
{
assert(hp);
return hp->size;
}
HPDataType HeapTop(HP* hp)
{
assert(hp);
assert(!HeapEmpty(hp));
return hp->a[0];
}
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[child], &a[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void HeapSort(int* a, int n)
{
// 把a构建成小堆
// 方法1:
/*for (int i = 1; i < n; ++i)
{
AdjustUp(a, i);
}*/
// 方法2:
// O(N)
for (int i = (n - 1 - 1) / 2; i >= 0; --i)
{
AdjustDown(a, n, i);
}
// 依次选数,调堆
// O(N*logN)
for (int end = n - 1; end > 0; --end)
{
Swap(&a[end], &a[0]);
// 再调堆,选出次小的数
AdjustDown(a, end, 0);
}
}
int main()
{
//TestTopk();
int a[] = { 70, 56, 30, 25, 15, 10, 75 };
for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
printf("%d ", a[i]);
}
printf("\n");
HeapSort(a, sizeof(a) / sizeof(a[0]));
for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
{
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
总结
以上就是今天要讲的内容,本文介绍堆排序的应用之一:堆排序!
如果我的博客对你有所帮助记得三连支持一下,感谢大家的支持!