当前位置:网站首页>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]);
}
}边栏推荐
- 在转化词向量之前先转化为AST再转化为词向量的实现方法
- shell---函数
- Blue Bridge Cup square filling number
- Multiprocessing (multiprocessing)
- MySQL queries all descendant nodes under the parent node. When querying the user list, it is processed by multi-level (company) departments. According to reflection, it recurses the tree structure too
- MySQL build database Series (I) -- download MySQL
- 关于正则的教程
- DHCP service
- MOOC翁恺 C语言 第三周:判断与循环:1.判断
- Blue bridge code error ticket
猜你喜欢

The.Joernindex database has no content after Joern runs

MySQL查询父节点下面的所有子孙节点,查询用户列表时多级(公司)部门处理,根据反射,递归树形结构工具类

WiFi one click connection configuration of raspberry pie

RAID disk array

VLAN的配置

VNC Timed out waiting for a response from the computer

win下安装nessus

Shell -- first day homework

远程访问云服务器上Neo4j等服务的本地网址

VLAN configuration
随机推荐
Sysevr environment configuration: joern-0.3.1, neo4j-2.1.5, py2neo2.0
Tutorial on regularization
Canvas drawing 2
Install Nessus under win
MySQL build database Series (I) -- download MySQL
Method of decomposing path into directory name and file name
Differences and relationships among NPM, Yran and NPX
Implementation method of converting ast into word vector before converting word vector
shell---循环语句练习
Freemaker merges cells, uses if and else tags, and processes null and empty strings
Standard C language learning summary 6
Shell --- conditional statement practice
Shell--- circular statement practice
在转化词向量之前先转化为AST再转化为词向量的实现方法
bond模式配置
Media set up live broadcast server
Shell -- first day homework
视频格式基础知识:让你了解MKV、MP4、H.265、码率、色深等等
Raspberry pie serial port
Esxi arm edition version 1.10 update