当前位置:网站首页>堆-堆排序-TopK
堆-堆排序-TopK
2022-06-27 10:24:00 【atong~】
大堆:父节点比左右子孩子大
小堆:父节点比左右子孩子小
大堆向下调整

向下调整算法
注意结束
- 是左右子树超过数组end
- 到了合适位置直接break出去
问题:end是数组元素个数还是最后一个元素所在下标?
最后一个元素所在下标
错误代码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;
}
}
}
更正代码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;
}
}
}
}
最终代码3
//建大堆
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;
}
}
}
大堆向上调整

向上调整算法
- 结束条件:child<=0;
- 在end处插入一个元素,与父节点向上比较,孩子大就调换,孩子不大于父亲就break
代码 大堆
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;
}
}
}
建堆-时间复杂度分析

1、向下建堆

代码:向下调整算法
//向下调整
for(int i=(n-1-1)/2;i>=0;i--)
{
AdJustDown(a, n, i);
}
时间复杂度分析(向下调整)

2、向上建堆
代码:向上调整算法
// 向上调整
for(int i=0;i<sizeof(a)/sizeof(int);i++)
{
AdJustUp(a,i);
}
建堆复杂度分析(向上调整)

atong知识分享-TopK问题
1、如何在n个数中选出前K大/小的数

2、堆实现
N个数,选出前K大的数(建小堆-小堆堆顶)

分析
建小堆,a[i]大于堆顶,交换向下调整,大的会往下调整到堆底,小数往上冒,当i遍历到N时,堆顶就是第K大的元素
3、时间复杂度分析
- 建k个数堆时间复杂度
向下调整建堆K个数:O(K)
- 后边数据比较调整
最坏情况,N个数从小到大排,从K+1个数开始,每个数都比堆顶元素大,K个元素有logK层,每个数调整logK次,都调整到堆底时间复杂度为:O((N-K)*logK)
*总体时间复杂度:O(K+(N-K)logK)
4、代码实现
void TestTopK(int* a, int n, int k)
{
//建堆K个数 a[k-1] 它的父亲是(k-1-1)/2 向下调整建堆
for(int i=(k-1-1)/2;i>=0;i--)
{
AdJustDown(a, k, i);
}
/* 若不破坏原数组 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);
}
}
}
边栏推荐
猜你喜欢

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

Comparison between new and old interfaces

Working at home is more tiring than going to work at the company| Community essay solicitation

Product strength benchmarking seal /model 3, with 179800 pre-sales of Chang'an dark blue sl03

Mail system (based on SMTP protocol and POP3 protocol -c language implementation)

CPU design (single cycle and pipeline)

通俗易懂理解樸素貝葉斯分類的拉普拉斯平滑

详解各种光学仪器成像原理
![leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]](/img/70/3954b0871cc31d24ae016eb99d871e.png)
leetcode:968. Monitor the binary tree [tree DP, maintain the three states of each node's subtree, it is very difficult to think of the right as a learning, analogous to the house raiding 3]
Test how students participate in codereview
随机推荐
Xiaobai can also understand how the basic network 03 | OSI model works (classic push)
LVI Sam summary
DNS standby server information, DNS server address (how many DNS preferred and standby are filled in)
C apprentissage des langues - jour 12.
[so official interview] Why do developers using rust love it so much
Ubuntu手动安装MySQL
Product strength benchmarking seal /model 3, with 179800 pre-sales of Chang'an dark blue sl03
测试同学怎么参与codereview
go-zero微服务实战系列(七、请求量这么高该如何优化)
R language plot visualization: plot to visualize the two-dimensional histogram contour map, add numerical labels on the contour lines, customize the label font color, and set the mouse hover display e
基于swiftadmin极速后台开发框架,我制作了菜鸟教程[专业版]
使用Karmada实现Helm应用的跨集群部署【云原生开源】
C语言学习-Day_05
【TcaplusDB知识库】Tmonitor后台一键安装介绍(一)
mongodb跨主机数据库拷贝以及常用命令
leetcode:522. Longest special sequence II [greed + subsequence judgment]
[cloud enjoys freshness] community weekly · vol.68- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + business realization
[从零开始学习FPGA编程-47]:视野篇 - 第三代半导体技术现状与发展趋势
Concepts of concurrency, parallelism, asynchronism, synchronization, multithreading and mutual exclusion
Error im002 when Oracle connects to MySQL