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

#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. django 后台静态文件不显示

    原文链接 https://my.oschina.net/VASKS/blog/874270 django admin svg 不显示.后台显示 xx.svg 200 但浏览器就是不显示. 百度了一圈, ...

  2. RGB-D(深度图像) & 图像深度

    RGB-D(深度图像)   深度图像 = 普通的RGB三通道彩色图像 + Depth Map   在3D计算机图形中,Depth Map(深度图)是包含与视点的场景对象的表面的距离有关的信息的图像或图 ...

  3. 前端知识点回顾——mongodb和mongoose模块

    mongodb和mongoose模块 数据库 数据库有关系型数据库(MySQL)和非关系型数据库(mongodb),两者的语法和数据存储形式不一样. mySQL 关系型数据库 类似于表格的形式,每一条 ...

  4. [转][C#].Net反编译利器

    来自:https://www.cnblogs.com/zsuxiong/p/5117465.html 有以下8款非常不错的.Net反编译利器: 1.Reflector Reflector是最为流行的. ...

  5. centos6.5安装mysql(转载,亲测可用)

    如果要在Linux上做j2ee开发,首先得搭建好j2ee的开发环境,包括了jdk.tomcat.eclipse的安装(这个在之前的一篇随笔中已经有详细讲解了Linux学习之CentOS(七)--Cen ...

  6. cv2.bitwise_and的应用,

    import cv2 import numpy as np Load two images img1 = cv2.imread('messi.png') img2 = cv2.imread('logo ...

  7. HANA到MySQL数据同步方法!

    随着各行各业信息化建设的不断发展,异构数据库间的互通.汇聚,挖掘,分析逐渐被提上日程, TreeSoft数据库管理系统,实现了异构数据库的维护.监控.可视化.自动交换同步.目前支持MySQL,Orac ...

  8. python-Web-flask-数据库

    3 数据库: Flask-SQLAlchemy 安装及连接 pip install flask-sqlalchemy pip install flask-mysqldb # 数据库链接地址 app.c ...

  9. (IStool)删除新版本已废弃但可能会影响新版本运行的文件夹/文件

    需求:老版本服务器的某些文件或文件夹在新版本已弃用,新版本覆盖安装时需要将文件夹或文件删除 实现:覆盖安装完成后将对应目录下的文件删除 [InstallDelete] Type: files; Nam ...

  10. Insomni’hack CTF-l33t-hoster复现分析

    题目地址: https://github.com/eboda/insomnihack/tree/master/l33t_hoster 源码如下: <?php if (isset($_GET[&q ...