复杂度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大值.我们又知道主席树满足可加性,所以我们可以用树状数组来维护主席树,树状数组的每一个节点都可以开一颗主席树,然后一起做. 我们注意到树状数组的每一棵树都和 ...
随机推荐
- Flume-自定义 Source
Source 是负责接收数据到 Flume Agent 的组件. Source 组件可以处理各种类型.各种格式的日志数据,包括 avro.thrift.exec.jms.spooling direct ...
- win10设置开机开启数字锁定
windows10开机小键盘默认关闭,网上查询修改InitialKeyboardIndicators键值为2,或者80000002,经过实际测试,均无效,键值8000000002有效,是中间8个0,开 ...
- 50行代码写的一个插件,破解一个H5小游戏
小游戏链接:测测你的眼睛对色差的辨识度http://www.webhek.com/post/color-test.html?from=timeline 废话不多说,先放代码: window.onloa ...
- C之交换数据案例
//值传递 void swap(int i,int j){ printf("交换后:\n "); int tmp; tmp = i; i = j; j = tmp; } //引用传 ...
- c++ STL find search
#include <iostream>#include <algorithm>#include <deque>#include <list>#inclu ...
- Go单引号和双引号区别
首先做个测试,看下面那个选项是正确的: A. str:='abc'+'123'B. str:="abc"+"123"C. str:='123'+"ab ...
- Django:(08)序列化器
1.序列化和反序列化变量从内存中变成可存储或传输的过程称之为序列化,序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上.反过来,把变量内容从序列化的对象重新读到内存里称之为反序列 ...
- 【AMAD】django-debug-toolbar -- 一个可配置的panel,展示当前request/response的debug信息
简介 个人评分 简介 django-debug-toolbar1是一个django开发工具,可以在你开发django页面的时候展示一些当前请求的debug信息:  个人评分 类型 评分 实用性 ⭐️ ...
- webdriver(处理select下拉框元素)
"""处理下拉框""" from selenium import webdriver from selenium.webdriver.com ...
- 【VS开发】malloc申请内存错误分析
每个进程会有4G的虚拟地址空间, malloc得到的的地址都是虚拟地址, 并且当malloc的时候, 操作系统并不会将实际的内存分配给进程的, 所以malloc只会占用进程自身的虚拟地址空间.我以前也 ...