当前位置:网站首页>Heap heap sort TOPK
Heap heap sort TOPK
2022-06-27 11:15:00 【atong~】
A lot : The parent node is larger than the left and right child
Little heap : The parent node is smaller than the left and right child nodes
Adjust downward

Adjust the algorithm down
Pay attention to the end
- Is the left and right subtrees over the array end
- At the right place break get out
problem :end Is it the number of array elements or the subscript of the last element ?
The subscript of the last element
Error code 1、
void AdjustDown(int* a, int n)
{
assert(a);
int parent=0;
int left=2*parent+1;
int right=left+1;
while(left<end)
{
left=2*parent+1;
right=left+1;
if(a[left]>a[right])
{
Swap(&a[parent],&a[left]);
parent=left;
}
else
{
Swap(&a[parent],&a[right]);
parent=right;
}
}
}
Correction code 2
void AdjustDown(int* a, int n)
{
assert(a);
int parent=0,left=0,right=0;
while(left<end)
{
left=2*parent+1;
right=left+1;
if(a[left]>a[right])
{
if(a[parent]<a[left])
{
Swap(&a[parent],&a[left]);
parent=left;
}
else
{
break;
}
}
else
{
if(a[parent]<a[right])
{
Swap(&a[parent],&a[right]);
parent=right;
}
else
{
break;
}
}
}
}
The final code 3
// Build a pile
void AdjustDown(int* a, int n, int parent)
{
int child=parent*2+1;
while(child<end)
{
if(child+1<n&&a[child+1]>a[child+1])
{
child=child+1;
}
if(a[child]>a[parent])
{
Swap[&a[parent],&a[child]);
parent=child;
child=2*parent+1;
}
else
{
break;
}
}
}
Adjust it up

Adjust the algorithm up
- The end condition :child<=0;
- stay end Insert an element at , Compare with the parent node up , Children change when they are older , Children are no bigger than their father break
Code A lot
void AdJustUp(int* a, int child)
{
int parent=(child-1)/2;
while(child>0)
{
if(a[child]>a[parent])
{
Swap[&a[child],&a[parent]);
child=parent;
parent=(child-1)/2;
}
else
{
break;
}
}
}
Building the heap - Time complexity analysis

1、 Build a pile down

Code : Adjust the algorithm down
// Downward adjustments
for(int i=(n-1-1)/2;i>=0;i--)
{
AdJustDown(a, n, i);
}
Time complexity analysis ( Downward adjustments )

2、 Build a pile up
Code : Adjust the algorithm up
// Adjust up
for(int i=0;i<sizeof(a)/sizeof(int);i++)
{
AdJustUp(a,i);
}
Reactor building complexity analysis ( Adjust up )

atong Knowledge sharing -TopK problem
1、 How to be in n Before the number is selected K Big / Small number

2、 Heap implementation
N Number , Before election K Large number ( Build a small pile - Small pile top )

analysis
Build a small pile ,a[i] Larger than the top of the pile , Swap downward adjustment , The large ones will be adjusted down to the bottom of the pile , Decimals go up , When i Traversing N when , The top of the pile is the second K Big elements
3、 Time complexity analysis
- build k Number of heaps time complexity
Adjust the build down K Number :O(K)
- The following data are compared and adjusted
The worst ,N The number is from small to large , from K+1 The number begins , Each number is larger than the top of the heap element ,K The elements are logK layer , Each number is adjusted logK Time , The time complexity is adjusted to the bottom of the heap :O((N-K)*logK)
* Overall time complexity :O(K+(N-K)logK)
4、 Code implementation
void TestTopK(int* a, int n, int k)
{
// Building the heap K Number a[k-1] Its father is (k-1-1)/2 Adjust the build down
for(int i=(k-1-1)/2;i>=0;i--)
{
AdJustDown(a, k, i);
}
/* If you don't destroy the original array int* MinHeap = (int*)malloc(sizeof(int)*k); assert(MinHeap); for (int i = 0; i < k; ++i) { MinHeap[i] = a[i]; } for (int i = (k - 1 - 1) / 2; i >= 0; --i) { AdjustDwon(kMinHeap, k, i); } */
for(int i=k+1; i<n, i++)
{
if(a[i]>a[0])
{
Swap(&a[0], &a[i]);
AdJustDown(a, k, 0);
}
}
}
边栏推荐
- deep learning statistical arbitrage
- “全班29人24人成功读研”冲上热搜!剩下的5个人去哪了?
- 【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
- QStyle类用法总结(二)
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
- 机器学习系统在生产中的挑战
- [tcapulusdb knowledge base] Introduction to tcapulusdb system management
- [tcapulusdb knowledge base] tcapulusdb system user group introduction
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - Introduction to creating game area
猜你喜欢

Summary of qstype class usage (II)

【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)

红包雨: Redis 和 Lua 的奇妙邂逅

堆-堆排序-TopK

Qstype implementation of self drawing interface project practice (I)

【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍

记一次 .NET 某物管后台服务 卡死分析

杰理之串口通信 串口接收IO需要设置数字功能【篇】
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction](/img/da/449a1e215885597a67344e2a6edf0f.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction

Leetcode 729. My schedule I (awesome, solved)
随机推荐
Working at home is more tiring than going to work at the company| Community essay solicitation
防止被00后整顿?一公司招聘要求员工不能起诉公司
Prevent being rectified after 00? I. The company's recruitment requires that employees cannot sue the company
【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
Metadata of database
15+城市道路要素分割应用,用这一个分割模型就够了!
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
在外企远程办公是什么体验? | 社区征文
【TcaplusDB知识库】Tmonitor系统升级介绍
【TcaplusDB知识库】TcaplusDB-tcapsvrmgr工具介绍(一)
【TcaplusDB知识库】TcaplusDB机型管理介绍
Jerry's adding timer interrupt [chapter]
QStyle类用法总结(二)
MQTT协议栈原理及交互流程图
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (II)
Deep understanding of happens before principle
杰理之睡眠以后定时唤醒系统继续跑不复位【篇】
go-zero微服务实战系列(七、请求量这么高该如何优化)
[tcapulusdb knowledge base] Introduction to tcapulusdb data import
[tcapulusdb knowledge base] Introduction to tmonitor stand-alone installation guidelines (I)