利用快速排序的方法进行:

#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大值的更多相关文章

  1. POJ2985 The k-th Largest Group[树状数组求第k大值+并查集||treap+并查集]

    The k-th Largest Group Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 8807   Accepted ...

  2. [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 ...

  3. 求数列中第K大的数

    原创 利用到快速排序的思想,快速排序思想:https://www.cnblogs.com/chiweiming/p/9188984.html array代表存放数列的数组,K代表第K大的数,mid代表 ...

  4. [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 ...

  5. [经典算法题]寻找数组中第K大的数的方法总结

    [经典算法题]寻找数组中第K大的数的方法总结 责任编辑:admin 日期:2012-11-26   字体:[大 中 小] 打印复制链接我要评论   今天看算法分析是,看到一个这样的问题,就是在一堆数据 ...

  6. HDU 2639(01背包求第K大值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2639 Bone Collector II Time Limit: 5000/2000 MS (Jav ...

  7. POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)

    题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...

  8. 查找数组中第k大的数

    问题:  查找出一给定数组中第k大的数.例如[3,2,7,1,8,9,6,5,4],第1大的数是9,第2大的数是8-- 思考:1. 直接从大到小排序,排好序后,第k大的数就是arr[k-1]. 2. ...

  9. 动态求区间K大值(权值线段树)

    我们知道我们可以通过主席树来维护静态区间第K大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...

随机推荐

  1. 数据分析 - Power BI

    BI 目的 单表的展示有限很多的时候只能体现现象, 仅仅进行监控级别没问题 但是就数据分析而言实在不够用, 大部分的事情需要多表多图展示才可以通过现象深入挖掘诱因 BI 安装 这里使用 microso ...

  2. [maven]idea+maven的多项目依赖

    如下两个项目: test-main test-utils 其中test-main需要引用test-utils. 最终效果如下: 实现步骤: 1:新建一个Empty Project作为框架项目 输入框架 ...

  3. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_12-webpack研究-webpack安装

    npm默认安装配置的路径配置在nodejs的node_modules目录 j加上 -g 就是全局安装 后面只写webpack默认安装的是最新版本 指定版本号 视频中建议指定版本号进行安装

  4. spark入门备忘---1

    import org.apache.spark.{SparkContext, SparkConf} import scala.math.random /** * 利用spark进行圆周率的计算 */ ...

  5. labelme

    项目:https://github.com/wkentaro/labelme?tdsourcetag=s_pcqq_aiomsg 说明:https://www.bilibili.com/video/a ...

  6. MIGO 收货

    ls_code-gm_code = '01'. 01 - MB01 - Goods Receipts for Purchase Order 02 - MB31 - Goods Receipts for ...

  7. iOS开发之——keychain使用

    iOS的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书等)的方式.每个ios程序都有一个独立的keychain存储.从ios 3.0开始,跨程序分享keychain变得可行. 使 ...

  8. ldap客户端工具ldap admin tool

    官网下载:非免费软件,自己破解 http://www.ldapbrowsermac.com/

  9. Flutter 的异步机制Future

    Dart是一个单线程语言,可以理解成物理线路中的串联,当其遇到有延迟的运算(比如IO操作.延时执行)时,线程中按顺序执行的运算就会阻塞,用户就会感觉到卡顿,于是通常用异步处理来解决这个问题. Dart ...

  10. VAO和VBO

    我想大家都已经熟悉VBO了吧.在GL3.0时代的VBO大体还是处于最重要的地位,但是与此同时也出现了不少新的用法和辅助役,其中一个就是VAO.本文大致小记一下这两者的联系,帮助大家理解一下这个角色.— ...