当前位置:网站首页>Sort 4-heap sort and massive TOPK problem
Sort 4-heap sort and massive TOPK problem
2022-07-28 16:31:00 【World_ living】
One 、 Heap sort
(1) The heap is a complete binary tree
(2) The value of each node of the heap is greater than or equal to its left and right child nodes, which is called a heap
(3) The value of each node of the heap is less than or equal to its left and right child nodes, which is called small heap
Pile up : Those with the above conditions are called heaps 
We can see that the top of the pile is the maximum , The top of a small heap is the minimum .
Note from the heap : The following node must be the largest ( Small ) person .
The structure of the pile :
Take the above pile as an example : If you store a large number of sequence traversals into an array , Then the following relationship is satisfied 
Left child coordinates =2 Father coordinates +1
Right child mark =2 Father coordinates +2
Let's talk about this heap structure , Its purpose is for heap sorting .
. Heap sort algorithm
Heap sort : The sequence to be sorted is constructed into a large ( Small ) Pile up , The alignment of the whole sequence ( Small ) Value is the root node at the top of the heap . Take it away , And then put the rest of n-1 A sequence is reconstructed into a heap , You can get the second largest ( Small ) Value , So again and again , Then we can get an ordered sequence .
Downward adjustment of heap :
Premise : The left and right subtrees must be heaps
By adjusting down , Pile up the whole tree 
- Choose the one between the left and right children .
- Young children are compared with their father .
- If a young child is younger than his father , Exchange with my father , And continue to adjust the original position of the original child down .
- If a young child is older than his father , There is no need to deal with , Adjustment complete .
void Swap(int *p,int *q)
{
int temp = *p;
*p = *q;
*q = temp;
}
void AdJustDown(int *arr,int n,int parent)
{
int child = parent * 2 + 1;
while (child < n)
{
if (child+1<n && arr[child + 1] < arr[child])
{
child++;
}
if (arr[parent] > arr[child])
{
Swap(&arr[parent],&arr[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
int main()
{
int arr[] = { 27, 15, 19, 18, 28, 34, 65, 44 };
int sz=sizeof(arr)/sizeof(arr[0]);
AdJustDown(arr,sz,0);
for (int i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
}
Build a pile :
In ascending order to build a lot of , Build small piles in descending order .
- Build a pile
- Bottom up , From right to left

Build a pile yes Bottom up , From right to left 
#include<stdio.h>
void Swap(int *p,int *q)
{
int temp = *p;
*p = *q;
*q = temp;
}
void AdJustDown(int *arr,int n,int parent)
{
int child = parent * 2 + 1;
while (child < n)
{
if (child+1<n && arr[child + 1] < arr[child])
{
child++;
}
if (arr[parent] > arr[child])
{
Swap(&arr[parent],&arr[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void HeaqSort(int *arr,int n)
{
// Build a pile , Bottom up , From right to left .
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdJustDown(arr, n, i);
}
// Heap sort
//int end = n - 1;
//while (end>0)
//{
//Swap(&arr[0],&arr[end]);
//AdJustDown(arr,end, 0);
//end--;
//}
}
int main()
{
int arr[] = { 27, 15, 11, 18, 28, 4, 65, 44 };
int sz=sizeof(arr)/sizeof(arr[0]);
HeaqSort(arr, sz);
for (int i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
}
Heap sort :
void HeaqSort(int *arr,int n)
{
// Building the heap
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdJustDown(arr, n, i);
}
// Heap sort
int end = n - 1;
while (end>0)
{
Swap(&arr[0],&arr[end]);
AdJustDown(arr,end, 0);
end--;
}
}
The downward adjustment method is complex :log2n
Reactor building time complexity :O(N)
The time of heap sorting is complex :O(N*log2N)
Build the pile as follows :
We use full binary tree to calculate , The height of the stack is h, Suppose the height of each floor is hi, The number of nodes in each layer is ni, The time of building the reactor is complex :
so : The complexity of time is O(N)
Two 、 Massive TopK problem

Train of thought : Direct heap sort
Time complexity :O(Nlog2N)
Train of thought two : Time complexity :O(Nlog2k), Spatial complexity O(K)
- First put the array K Data , Build a pile
- And then the rest N-K Number , Compare with the data on the top of the pile , If it is smaller than the data at the top of the heap , Replace the data at the top of the heap , Then adjust the heap ( This heap adjustment is before the adjustment K One of the ).
- Then in front of the pile K The number is K Small value
void Swap(int *p,int *q)
{
int temp = *p;
*p = *q;
*q = temp;
}
void AdJustDown(int *arr,int n,int parent)
{
int child = parent * 2 + 1;
while (child < n)
{
if (child+1<n && arr[child + 1] > arr[child])
{
child++;
}
if (arr[parent] < arr[child])
{
Swap(&arr[parent],&arr[child]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void HeaqSort(int *arr,int n)
{
// Building the heap
for (int i = (n - 1 - 1) / 2; i >= 0; i--)
{
AdJustDown(arr, n, i);
}
}
int* getLeastNumbers(int* arr, int arrSize, int k, int* returnSize)
{
int i=k;
HeaqSort(arr,k);
for(int k=0;k<arrSize;k++)
{
printf("%d ",arr[k]);
}
while(i<arrSize)
{
if(arr[i]<arr[0])
{
Swap(&arr[i],&arr[0]);
AdJustDown(arr,k,0);
i++;
}
else
{
i++;
}
}
int*retarr=(int *)malloc(sizeof(int)*k);
for(int j=0;j<k;j++)
{
retarr[j]=arr[j];
}
*returnSize=k;
return retarr;
}
Thank you for making mistakes .
边栏推荐
- Implementation of skip table
- Use py to automatically generate weekly reports based on log records
- 一小时内学会Abaqus脚本编程秘籍
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
- 后台弹出layer提示
- Why do most people who learn programming go to Shenzhen and Beijing?
- R语言ggplot2可视化绘制线图(line plot)、使用gghighlight包突出高亮线图中满足组合判断条件的线图(satisfies both condition A and B)
- HM二次开发 - Data Names及其使用
- ANSYS二次开发 - MFC界面调用ADPL文件
- PHP image upload
猜你喜欢
随机推荐
为什么学编程的人大多数都去了深圳和北京?
Pop up layer prompt in the background
Why do most people who learn programming go to Shenzhen and Beijing?
我在上海偶遇数字凤凰#坐标徐汇美罗城
Geodetic coordinate system to Martian coordinate system
1. Simple command line connection to database
一大早支付宝来短信说你中“奖”了?处理服务器挖矿病毒 - kthreaddi
LwIP development | socket | TCP | client
使用js直传oss阿里云存储文件,解决大文件上传服务器限制
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
R language uses ggpairs function of ggally package to visualize pairwise relationship graph of grouping multivariable, set alpha parameter to change image transparency, diagonal continuous variable de
李宏毅《机器学习》丨4. Deep Learning(深度学习)
Li Hongyi, machine learning 5. Tips for neural network design
The little red book of accelerating investment, "rush to medical treatment"?
flashfxp 530 User cannot log in. ftp
Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
Curl returns blank or null without output. Solve the problem
PHP image synthesis technology
2021-10-21 notes
Fifth uncle's thinking








