網(wǎng)站首頁 編程語言 正文
問題
在一百萬個(gè)數(shù)據(jù)中,求出最大的k個(gè)數(shù)字,怎么效率高。
1. 將一百萬個(gè)數(shù)據(jù)排序,承接上一篇的堆排序,時(shí)間復(fù)雜度為O(N * LogN)。但是顯然這并不是最優(yōu)解。
2. 一百萬個(gè)數(shù)據(jù)放入一個(gè)數(shù)組中,將其視為一個(gè)完全二叉樹,并用向下調(diào)整算法將其調(diào)整為一個(gè)大堆/小堆,然后Top/Popk次,即可求出前K個(gè)最大/最小的數(shù)字,時(shí)間復(fù)雜度為:O(N + K*LogN)
3. 用正確的堆處理TopK算法: 先假設(shè)求最大的K個(gè)數(shù)字,則建立大小為K的小根堆,然后在一百萬-k個(gè)數(shù)據(jù)中,逐個(gè)遍歷,若某個(gè)數(shù)據(jù)比小根堆的堆頂元素大,則替換掉堆頂元素,然后向下調(diào)整,使得這個(gè)堆重新變回一個(gè)小根堆。 時(shí)間復(fù)雜度為:O(K + (N-k)*LogK)
其實(shí)相較于2,3并沒有時(shí)間上的很大提升,但是3在空間復(fù)雜度上有了巨大提升,2的空間為O(N),3為O(K)。 折中思考,3方法是求數(shù)據(jù)量較大的數(shù)據(jù)集合中前K個(gè)最大值/最小值的最佳方法
分析
求K個(gè)最大值,建小堆,是因?yàn)椋舯闅v途中遇到了那K個(gè)數(shù)中的某一個(gè),他一定比堆頂元素大,然后替換進(jìn)去之后,向下調(diào)整,可以使得這個(gè)數(shù)字置于這個(gè)小根堆的底部。從而達(dá)到目的。
代碼實(shí)現(xiàn)
void AdjustUp(int* a, int child)
{
int parent = (child - 1) / 2;
while (child > 0)
{
if (a[child] > a[parent])
{
swap(a[parent], a[child]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void AdjustDown(int* a,int size, int parent) // size 是總大小,parent是從哪里開始向下調(diào)整
{
int child = parent * 2 + 1;
while (child < size)
{
if (child + 1 < size && 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 Print_Heap_Topk(int* a, int n, int k)
{
int* KMaxHeap = new int[k]; // 最大堆存最小的K個(gè)數(shù)
for (int i = 0; i < k; ++i)
{
KMaxHeap[i] = a[i];
}
for (int i = (k - 1 - 1) / 2; i >= 0; --i)
{
AdjustDown(KMaxHeap, k, i);
}
for (int i = k; i < n; ++i)
{
if (a[i] < KMaxHeap[0])
KMaxHeap[0] = a[i];
AdjustDown(KMaxHeap, k, 0);
}
for (int i = 0; i < k; ++i)
{
cout << KMaxHeap[i] << " ";
}
cout << endl;
}
void test_topk()
{
int n = 10000;
int* a = new int[n];
srand(time(0));
for (int i = 0; i < n; ++i)
a[i] = rand() % 1000000;
a[5] = 1000000 + 1;
a[1231] = 1000000 + 2;
a[531] = 1000000 + 3;
a[5121] = 1000000 + 4;
a[120] = 1000000 + 5;
a[99] = 1000000 + 6;
a[0] = 1000000 + 7;
a[76] = 1000000 + 8;
a[423] = 1000000 + 9;
a[3144] = 1000000 + 10;
a[333] = -100;
a[999] = -200;
a[777] = -500;
a[888] = -800;
a[111] = -1000;
a[798] = -1;
a[1111] = -250;
a[2222] = -350;
a[3333] = -450;
a[4444] = -550;
Print_Heap_Topk(a, n, 10);
}
int main()
{
test_topk();
return 0;
}
原文鏈接:https://blog.csdn.net/i777777777777777/article/details/125034312
相關(guān)推薦
- 2022-10-26 C#?form-data上傳圖片流到遠(yuǎn)程服務(wù)器的詳細(xì)代碼_C#教程
- 2024-07-15 arthas操作spring被代理目標(biāo)對(duì)象命令速查
- 2022-11-01 Flask模板渲染與Get和Post請(qǐng)求詳細(xì)介紹_python
- 2022-10-22 Python?NumPy教程之?dāng)?shù)組的創(chuàng)建詳解_python
- 2024-01-09 使用<scope>import</scope>解決Maven項(xiàng)目單繼承問題
- 2022-05-20 springCloud_Nacos服務(wù)搭建
- 2022-06-09 ASP.NET?Core使用EF創(chuàng)建模型(必需和可選屬性、最大長度、并發(fā)標(biāo)記、陰影屬性)_實(shí)用技巧
- 2022-03-28 一篇文章帶你學(xué)習(xí)python的函數(shù)與類_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支