当前位置:网站首页>Thinking of finding the k-th largest element in O (n) by fast sorting
Thinking of finding the k-th largest element in O (n) by fast sorting
2022-06-09 21:17:00 【Zhaohan】
O(n) In time complexity, find the second... In the unordered array K Big element .
such as ,4, 2, 5, 12, 3 Such a set of data , The first 3 The big element is 4.
We choose the array interval A[0...n-1] Last element of A[n-1] As pivot, An array A[0...n-1] In situ zoning , So the array is divided into three parts ,A[0...p-1]、A[p]、A[p+1...n-1]. If p+1=K, that A[p] It's the element that requires the solution ; If K>p+1, Note No K Big elements appear in A[p+1...n-1] Section , Let's follow the above idea recursively in A[p+1...n-1] Look for... In this interval . Empathy , If K<p+1, Then we are A[0...p-1] Interval search .
Let's see , Why the time complexity of the above solution is O(n)?
First partition lookup , We need to set the size of n To perform partitioning operations , Need to traverse n Elements . Second partition lookup , We just need to set the size of n/2 To perform partitioning operations , Need to traverse n/2 Elements . By analogy , The number of partition traversal elements is respectively 、n/2、n/4、n/8、n/16.…… Until the interval is reduced to 1.
If we add up the number of elements per partition traversal , Namely :n+n/2+n/4+n/8+...+1. This is the sum of an equal sequence of numbers , The final sum is equal to 2n-1. therefore , The time complexity of the above solution is O(n).
You might say , I have a stupid idea , Take the minimum value in the array every time , Move it to the front of the array , Then continue to find the minimum value in the remaining array , And so on , perform K Time , The data found is no K The big element ? however , Time complexity is not O(n) 了 , It is O(K * n).
You might say , The coefficient in front of time complexity can be ignored ?O(K * n) No is equal to O(n) Do you ? This can't be equated so simply . When K Is a small constant , such as 1、2, The best time complexity is really O(n); But when K be equal to n/2 perhaps n when , The worst-case time complexity is O(n2) 了 .
边栏推荐
- The difference between CompareTo and compare
- GBase8s数据库select子句1
- Latex mathematical symbols Encyclopedia
- Configuration du serveur DHCP et de la connexion client
- Go 调用 Openstack API 的 几个简单的 example
- Gbase8s database select Clause 1
- 强烈推荐:数据标注平台doccano----简介、安装、使用、踩坑记录
- [cf] 797 div3 E. Price Maximization
- GBase 8s 扩展外连接
- maximum likelihood estimation
猜你喜欢

二叉搜索树

Analysis of source code data anti leakage solution

Sort - quick sort

paddleNLP-BUG和一些细节记录【一】

源代码数据防泄露解决方案分析

Typescript variable declaration

Unbutu configuring DHCP server and client connections

Idea:new no class

Summary of linear regression
![Fedformer:frequency enhanced decomposedtransformer for long term series forecasting[still learning...]](/img/58/d73cbaaeefc255816fe8cdc7165419.png)
Fedformer:frequency enhanced decomposedtransformer for long term series forecasting[still learning...]
随机推荐
03 Wireshark TCP
队列在线程池中的应用
Comprendre le go des modules go. MOD et go. SUM
03 Wireshark TCP
Make money by doing sideline work. These popular we media platforms have made a lot of profits
Why rewrite equals and hashcode?
How Bi makes SaaS products have a "sense of security" and "sensitivity" (Part I)
[cf] 797 div3 E. Price Maximization
Tke builds efk log service
分享 16 个有用的 TypeScript 和 JS 技巧
Typescript variable declaration
mmdetection训练自己的COCO数据集及常见问题
Paddlenlp--uie (II) -- fast performance improvement with small samples (including doccona label)
ASP. Net mobile terminal inventory system, source code sharing
Understand go modules' go Mod and go sum
快递单信息抽取【三】--五条标注数据提高准确率,仅需五条标注样本,快速完成快递单信息任务
Go 调用 Openstack API 的 几个简单的 example
Numpy duplicate data
QT database application 21 data grouping export
unbutu配置DHCP服务器及客户端连接