复杂度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大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...
随机推荐
- Resize image online 调整图片大小
https://resizeimage.net/ 第一步,先上传图片 第二步,可选的(裁剪图片),这里的type的ratio是指图片的宽高比例(和图片最后的size大小无关) 3. Rotate yo ...
- recyclerView 嵌套 recyclerView 点击被拦截
上层recyclerView.setLayoutFrozen(true);下层可以接收到点击事件.
- Event---事件详解
1.焦点事件 焦点:使浏览器能够区分用户输入的对象,当一个元素有焦点的时候,那么他就可以接收用户的输入. 可以通过以下方式给元素设置焦点: 点击.tab.js 不是所有元素都能够接收焦点的,能够响应用 ...
- 论好的代码习惯的养成/做一个优雅的coder
1.先说一下以前被滴滴大佬教育的事情: 以前写代码的时候,因为只需要取特定的几个字段,所以经常这么写 //Request $request for example $parameters = $req ...
- mysql连接工具记录
港优: Sequel pro
- Python实现按照指定要求逆序输出一个数字的方法
Python实现按照指定要求逆序输出一个数字的方法 这篇文章主要介绍了Python实现按照指定要求逆序输出一个数字的方法,涉及Python针对字符串的遍历.判断.输出等相关操作技巧,需要的朋友可以参考 ...
- kubernetes 之kubelet客户端证书过期问题处理 KubeClientCertificateExpiration apiserver (monitoring/k8s warning) Kubernetes API certificate is expiring in less than 7 days.
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB4gAAAKMCAYAAAAZj+XuAAABfGlDQ1BJQ0MgUHJvZmlsZQAAKJFjYG ...
- leetcode 四数之和
这里我们可以考虑将 n 数之和降低为一个数加上 n-1 数之和的问题.依次降低 ,最低是二数之和的问题 ,二数之和问题容易解决.主要在于从 n 到 n-1 的过程需要理解 :下列代码中前几个 if 是 ...
- Kali中安装 Shodan
工具介绍 Shodan 是一个搜索引擎,但它与 Google 这种搜索网址的搜索引擎不同,Shodan 是用来搜索网络空间中在线设备的,你可以通过 Shodan 搜索指定的设备,或者搜索特定类型的设备 ...
- 在先电IAAS平台中,搭建先电bigdata平台
以两台节点为例来组件Hadoop分布式集群,这里采用的系统版本为Centos7,如下表所示: 主机名 内存 硬盘 IP地址 角色 master 8192MB 100G 192.168.200.131 ...