Heap application two: heap sorting
Article directory
foreword
`
The application of the heap (heap sorting) will involve a lot of knowledge about the heap: if you don’t understand, you can refer to: Simulation implementation of the heap
1. Topic
Use heaps to achieve ascending and descending order (build large and small heaps respectively)
Topic: Arrange the numbers 20 17 4 16 5 3 in ascending order! Requirement: Time complexity optimized to: O(1)
2. Idea + diagram
1. Idea 1:
The code is as follows (example):
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. Idea 2:
For the convenience of drawing, we sorted the second idea: 20 17 4 16
3. Source code
The code is as follows (example):
#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;
}
Summarize
The above is what I want to talk about today. This article introduces one of the applications of heap sorting: heap sorting!
If my blog is helpful to you, remember to support it three times, thank you for your support!