求前k个最小的数---类似快排思想的O(n)
来源:互联网 发布:80后和90后的区别 知乎 编辑:程序博客网 时间:2024/04/30 22:20
#include<iostream>#include<vector>#include<algorithm>using namespace std;int Partition(vector<int>&data,int headId,int tailId){int posSlow = headId-1,posFast=headId;for(;posFast<tailId;++posFast){if(data[posFast]<data[tailId]){++posSlow;swap(data[posSlow],data[posFast]);}}++posSlow;swap(data[posSlow],data[tailId]);return posSlow;}void FindKLeast(vector<int>&data,int headId,int tailId,int k){if(headId<tailId){int midId=Partition(data,headId,tailId);if(midId>k){FindKLeast(data,headId,midId-1,k);}else{if(midId<k){FindKLeast(data,midId+1,tailId,k);}}}}void FindKLeastNumbers(vector<int>&data,unsigned int k){int len = data.size();if(k>len){throw new std::exception("Invalid argument!");}FindKLeast(data,0,len-1,k);}int main(){int a[]={12,42,10,31,6,15};int k=3;vector<int>::iterator iter;vector<int> ivector(a,a+6);FindKLeastNumbers(ivector,k);cout<<"前k个最小的数为:"<<endl;for(iter = ivector.begin();iter<=ivector.begin()+k-1;iter++)cout<<*iter<<" ";cout<<endl;}
0 0
- 求前k个最小的数---类似快排思想的O(n)
- 快排划分思想的应用-求第k大数或者第k小的数(求前k大数或者前k小的数)
- 快排的思想求第K小的数
- 快排思想 求第(前)k大数
- 堆处理海量数据----求前k个最小的数--时间复杂度(n * log k)
- 从n个元素中找出第K小的数 利用快排的思想来实现
- 寻找第K大的数(快排思想)
- 快排算法及利用快排思想寻找第k大(小)的数
- N个数中前K个小的树(快排的改进)
- 快排思想找第k小的数
- 使用快排思想 求第n大的值
- O(n)时间复杂度求最小的k个数和第k小的数
- 面试题: 求N个数中前k个大的数(大数据)
- 求n个无序数的前k个小的数
- soj3102 O(n)求第k小的数
- 使用优先级队列求一个大数组中的前k个最大的数(或前k个最小的数)
- 求N个数中最小的K的数 O(NlogK)
- 求K个最小的数
- nyoj 711 最舒适的路线
- research in hard disk IO
- 委托,把方法作为参数
- C++ - 阻止派生类(derived class)重新定义虚函数(final&sealed)
- 第六周作业
- 求前k个最小的数---类似快排思想的O(n)
- 细说(int) , int.parse() , convert.int32() 区别
- Java <--> C#
- HDU Tour
- 程序员面试宝典_第13章_数据结构基础_排序算法小结(1)
- ccd相机介绍网址
- 原子操作的实现原理
- jQuery实现遮罩弹出对话框(仿天猫的删除对话框)
- jQuery和zTree实现的下拉树