复杂度n求数组的第K大值
利用快速排序的方法进行:
#include<iostream>
using namespace std;
int test()
{
int a = ;
return a;
} int quickSort(int* inputArray, const int left, const int right, int bigCount, int &result)
{
cout << endl;
cout << "right is: " << right << endl;
cout << "left is: " << left << endl;
if(left > right)
{
cout << "return -1~~~" << endl;
result = -;
return -;
}
//在某一次递归中,如果传入的左右两边相等,且要找第一大的数,那么就返回该值即可
if(left == right && bigCount == )
{
int tmp = inputArray[right];
cout << "inputArray[right] is: " << tmp << endl;
result = tmp;
return tmp;
}
int maxNum = ;
int i = left;
int j = right + ;//因为下面有个j--,为了能直接用,将这里的j加1,因此输入数组最后需要填充一个数 int pivot = inputArray[left];
//这里的循环进行一次遍历,在遍历的过程中,将左边大于pivot的元素以及右边小于pivot的元素进行交换,一次遍历之后,i与j相遇,此时再将pivot与j相互交换
//那么,此时处在pivot的左边的数都是小于pivot的,pivot处在右边的数都是大于pivot的
//这时候假设左边有m个数,右边有n个数,那么pivot就是第m+1小的数,倒数第n+1大的数
//假设要求第k大的数,只需要右边的个数为k-1即可
do
{
do i++; while(inputArray[i] < pivot);
do j--; while(inputArray[j] > pivot);
if(i < j)
swap(inputArray[i], inputArray[j]);
}
while(i < j);
swap(inputArray[j], inputArray[left]);
cout << endl << "================after one swap==================" << endl;
for(int i = ; i < ; i++)
{
cout << inputArray[i] << " ";
}
cout << endl;
int len = right - j + ;
cout << "j is: " << j << endl;
cout << "bigCount is: " << bigCount << endl;
cout << "len is: " << len << endl;
if(len < bigCount)//需要在左边继续寻找
{
int tmp = quickSort(inputArray ,left, j - , bigCount - len, result);
if(tmp != -)
return tmp;
}
else if(len > bigCount) //需要在右边继续寻找
{
int tmp = quickSort(inputArray, j + , right, bigCount, result);
if(tmp != -)
return tmp;
}
else
{
int tmp = inputArray[j];
cout << "inputArray[j] is: " << tmp << endl;
result = tmp;
return tmp;
}
}
int main()
{
int a[] = {, , , , , , , , , , };
cout << endl << "================origion==================" << endl;
for(int i = ; i < ; i++)
{
cout << a[i] << " ";
}
int rrrr = ;
int result = ;
cout<<endl;
result = quickSort(a, , , , rrrr);
cout<<endl<<endl;
cout << "rrrr is " << rrrr << endl;
cout << "result is " << result << endl;
cout<<endl<<endl;
cout << endl << "================result==================" << endl;
for(int i = ; i < ; i++)
{
cout << a[i] << " ";
}
return ;
}
复杂度n求数组的第K大值的更多相关文章
- POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]
The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8807 Accepted ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 求数列中第K大的数
原创 利用到快速排序的思想,快速排序思想:https://www.cnblogs.com/chiweiming/p/9188984.html array代表存放数列的数组,K代表第K大的数,mid代表 ...
- [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [经典算法题]寻找数组中第K大的数的方法总结
[经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26 字体:[大 中 小] 打印复制链接我要评论 今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...
- HDU 2639(01背包求第K大值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- 查找数组中第k大的数
问题: 查找出一给定数组中第k大的数.例如[3,2,7,1,8,9,6,5,4],第1大的数是9,第2大的数是8-- 思考:1. 直接从大到小排序,排好序后,第k大的数就是arr[k-1]. 2. ...
- 动态求区间K大值(权值线段树)
我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...
随机推荐
- [Oracle]察看一张表的约束 和 察看一张表的索引
--察看一张表的约束select table_name,constraint_name,constraint_type from user_constraints where table_name=u ...
- /proc/sys/net/ipv4/ip_conntrack_max
Things to know (best practices and “issues”) READ IT !!! — uWSGI 2.0 documentationhttps://uwsgi-docs ...
- 花椒直播基于golang的中台技术实践
https://github.com/gopherchina/conference/blob/master/2019/2.7%20花椒直播基于golang的中台技术实践%20-%20周洋.pdf 花椒 ...
- SQL-W3School-基础:SQL AND & OR 运算符
ylbtech-SQL-W3School-基础:SQL AND & OR 运算符 1.返回顶部 1. AND 和 OR 运算符用于基于一个以上的条件对记录进行过滤. AND 和 OR 运算符 ...
- 第一个smarty例子--分页显示数据
模板页index.tpl: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- web worker 的 self
A self object, which is the global object representing the worker in this scope. 对self对象的译法,未知妥否. // ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_10-freemarker静态化测试-基于模板文件静态化
把resource拷贝到test目录下 只保留文件夹结构和test1.ftl这个模板文件就可以了. 新建一个包 编写测试类 使用freemaker提供的方法生成静态文件 Configuration是i ...
- jenkins容器内修改root密码--ubuntu系统
http://www.voidcn.com/article/p-yvnoogkc-ng.html 由于jenkins官方镜像是ubuntu系统,所有啥的都用 sudo 换到root账号,然后登陆har ...
- Spring-Boot的第三方类库的依赖版本调整方法
springboot方式构建的工程,是dependencyManagement方式进行依赖包的版本管理, spring中有默认的版本,可以修改ext参数来调整版本 如下SpringBoot-2.2.x ...
- idea配置git的步骤
第一次git到GitHub过程 打开到项目 这样就不用再用git Bash敲命令了