当前位置:网站首页>快速排序·例题二
快速排序·例题二
2022-07-13 18:13:00 【AC-PEACE】
题目:
给定一个长度为 n 的整数数列,以及一个整数 k,请用快速选择算法求出数列从小到大排序后的第 k 个数。
输入格式
第一行包含两个整数 n 和 k。
第二行包含 n 个整数(所有整数均在 1∼10^9范围内),表示整数数列。
输出格式
输出一个整数,表示数列的第 k 小数。
数据范围
1≤n≤100000
1≤k≤n输入样例:
5 3 2 4 1 5 3输出样例:
3
代码:
#include<iostream>
using namespace std;
const int N = 100010;
int q[N];
void quick_sort(int q[], int l, int r)
{
if(l >= r) return;
int i = l - 1, j = r + 1;
int x = q[l + r >> 1];
while(i < j)
{
do i ++; while(q[i] < x);
do j --; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
quick_sort(q, l, j);
quick_sort(q, j + 1, r);
}
int main()
{
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i ++) scanf("%d", &q[i]);
quick_sort(q, 0, n - 1);
printf("%d", q[k - 1]);
return 0;
}
边栏推荐
- MongoDB 暴跌!!!
- 功能测试如何快速转向自动化测试
- 入行软件测试之后,如何持续精进?
- 后备高速缓存api的使用
- Fluent: environment construction and project creation
- Xiao Bai can understand tacotron's Chinese speech synthesis practice
- . Net core uses basic authentication Middleware
- Wechat applet development - (XI) navigator
- Make 2048 games with pyGame
- 实在智能获两大全球顶级市场调研机构双料认证,进入全球卓越阵营
猜你喜欢

Differential reasoning over a virtual knowledge base

设计稳定的微服务系统时不得不考虑的场景

leetcode 18. 四数之和

微信小程序开发—(八)canvas绘制图形

C primer plus learning notes - 4. File IO (input / output)

cocoscreator动画与粒子根据绘制路径移动

Prism navigation function

入行软件测试之后,如何持续精进?

RT_ Use of thread semaphores

How to continue to improve after software testing?
随机推荐
分账系统如何给连锁便利店带来交易效率革命?
Differential reasoning over a virtual knowledge base
sojson本地反调试原理解析
“电信级”运行多年,亚信科技推出核心交易数据库AntDB7.0
ui文件转换为py文件方法
How to continue to improve after software testing?
有关登录安全,测试人该知道些什么?
Remember that the app did not respond to UI automation once
Detailed explanation of the parameters of the find command
Design simulation of smart home monitoring system based on 51 single chip microcomputer (proteus simulation + source code + Report)
手动切换scan
小程序毕设作品之微信教室预约小程序毕业设计(3)后台功能
C primer plus learning notes - 4. File IO (input / output)
C# 再述值类型
小程序毕设作品之微信企业公司小程序毕业设计(5)任务书
. Net core web API using log4net logs
App测试流程及测试点
自动化测试工具-Playwright(快速上手)
JMeter 常用的几种断言方法,你会了吗?
L2: comparison between optimization and arbitrum