当前位置:网站首页>TOPK problem
TOPK problem
2022-07-28 07:13:00 【zhengyawen666】
One Problem description
topk The problem is n Before the number is selected k A big ( Small ) The number of (n>>k(n<<k)), Before election k Take a large number as an example
Two Ideas
1 Heap sort O(n*logn) But if n Very big words , There may not be so much space for heap sorting
2 Build a pile After selecting the data at the top of the heap every time, continue to build the heap for the remaining data , Until the screening is complete n A digital . Time complexity
O(n+logn*k) Time efficiency is relatively 1 better . But build n The space required for a number of heaps may be very large .
3 Yes n The first of the numbers k First build a small pile of numbers , And then we'll talk to n-k The data were compared , If this number is larger than the data at the top of the heap , Then go into the pile , Then the small pile discharged is the front k A small pile of big numbers , Then we'll do a heap sort , You can select the front k Big numbers . Time complexity O(k+logk*(n-k))
Compared with 2 The time complexity has not changed much , The real optimization is in space .
Code implementation :
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
void print(int* pa, int sz)
{
assert(pa);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", pa[i]);
}
printf("\n");
}
void swap(int* pa1, int* pa2)
{
int tmp = *pa1;
*pa1 = *pa2;
*pa2 = tmp;
}
void AdjustDown(int* pa, int size, int parent)
{
assert(pa);
int child = parent * 2 + 1;// The default is left child , Then take out the smaller one of the left and right children
while (child < size)
{
if (pa[child + 1] > pa[child] && child + 1 < size)// If the right child is older than the left child , Then the child +1, Default to older children , Don't care about children , Only care about the relatively larger one And only the right child can
{
child = child + 1;
}// Find younger children
if (pa[child] > pa[parent])
{
swap(&pa[child], &pa[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
void Adjustup(int* pa, int child)
{
assert(pa);
int parent = (child - 1) / 2;
while (child > 0)
{
if (pa[child] > pa[parent])
{
swap(&pa[child], &pa[parent]);
child = parent;
parent = (child - 1) / 2;
}
else
{
break;
}
}
}
void HeapDownSort(int* pa, int size)
{
assert(pa);
int i = 0;
for (i = (size - 1 - 1) / 2; i >= 0; i--)//size-1 Is the subscript of the last node , Then his parent node is (size-1-1)/2
{
AdjustDown(pa, size, i);
}// Pile up
for (i = 1; i <= size - 1; i++)
{
swap(&pa[0], &pa[size - i]);
AdjustDown(pa, size - i, 0);
}
}
void topk(int*pa,int sz,int k)
{
int i = 0;
for (i = 0; i < k; i++)
{
Adjustup(pa, i);
}// Before formation k A heap of numbers
for (i = k; i < sz; i++)
{
if (pa[k] > pa[0])
{
swap(&pa[k], &pa[0]);
AdjustDown(pa, k, 0);
}// If the subsequent number is larger than the data at the top of the heap , After exchanging them for so long, they adjusted downward , Can form before k There are a lot of the largest numbers
}
HeapDownSort(pa, sz);
}
int main()
{
int a[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 5;
int sz = sizeof(a) / sizeof(a[0]);
topk(a, sz,k);
for (int i = 0; i < k; i++)
{
printf("%d ", a[i]);
}
}边栏推荐
- Animation animation realizes the crossing (click) pause
- easypoi一对多,合并单元格,并且根据内容自适应行高
- NAT network address translation
- Detailed explanation of active scanning technology nmap
- Nrf51822 review summary
- MOOC Weng Kai C language week 5: 1. cycle control 2. multiple cycles 3. cycle application
- Implementation method of Bert
- Implementation method of converting ast into word vector before converting word vector
- DOM window related data, operations & BOM operations
- Reptile learning summary
猜你喜欢

Gd32f407 porting freertos+lwip

metasploit渗透ms7_010练习

GFS分布式文件系统

MOOC翁恺C语言 第六周:数组与函数:1.数组2.函数的定义与使用3.函数的参数和变量4.二维数组

freemarker合并单元格,if、else标签的使用,null、空字符串处理

爬虫学习总结

静态和浮动路由

MOOC Weng Kai C language week 3: judgment and circulation: 2. circulation

Freemaker exports word with tables and multiple pictures to solve the repetition and deformation of pictures

shell---函数
随机推荐
Codesensor: convert the code into AST and then into text vector
VNC Timed out waiting for a response from the computer
JS array method Encyclopedia
Standard C language learning summary 5
Method of decomposing path into directory name and file name
Easypoi export table with echars chart
Standard C language learning summary 6
Log in to Oracle10g OEM and want to manage the monitor program, but the account password input page always pops up
MySQL build database Series (I) -- download MySQL
VLAN configuration
VLAN的配置
Differences and relationships among NPM, Yran and NPX
在转化词向量之前先转化为AST再转化为词向量的实现方法
Blue Bridge Cup square filling number
Uniapp monitor whether the app has a network connection
codesensor:将代码转化为ast后再转化为文本向量
C language review (pointer)
Uni app double click event simulation
一个定时任务提醒工具
C language maze