当前位置:网站首页>Quick sort template
Quick sort template
2022-07-28 20:14:00 【Tiredd】
Title Description

#include <iostream>
using namespace std;
const int N = 100010;
int n, q[N];
void quick_sort(int l, int r)
{
if(l >= r) return ;
int x = q[l + r >> 1], i = l - 1, j = r + 1; // the reason being that do i ++, So stagger at the beginning 1 position , Then go in, just from l and r Start
while(i < j)
{
do i ++ ; while(q[i] < x); // Because after the exchange ,i Go ahead ,j Go back , So simply do i ++ To write
do j -- ; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
// Because the condition of out circulation is i > j, So either i == j, So divide the region l~j,j+1~r
quick_sort(l, j);
quick_sort(j + 1, r);
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
quick_sort(0, n - 1);
for(int i = 0; i < n; i ++ ) printf("%d ", q[i]);
}Extended topics : The first k Number
Title Description :

Code :
#include <iostream>
using namespace std;
const int N = 100010;
int n, q[N], k;
int ans;
void quick_sort(int l, int r, int k)
{
if(l >= r)
{
ans = q[l];
return ;
}
int x = q[l + r >> 1], i = l - 1, j = r + 1; // the reason being that do i ++, So stagger at the beginning 1 position , Then go in, just from l and r Start
while(i < j)
{
do i ++ ; while(q[i] < x); // Because after the exchange ,i Go ahead ,j Go back , So simply do i ++ To write
do j -- ; while(q[j] > x);
if(i < j) swap(q[i], q[j]);
}
// Because the condition of out circulation is i < j, So either i == j, So divide the region l~j,j+1~r
if(j - l + 1 >= k)
quick_sort(l, j, k);
else
quick_sort(j + 1, r, k - (j - l + 1));
}
int main()
{
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i ++ ) scanf("%d", &q[i]);
quick_sort(0, n - 1, k);
printf("%d", ans);
return 0;
}边栏推荐
- 党员故事|李青艾用漫画带动农民增收致富
- C+ + core programming
- Richpedia: A Large-Scale, Comprehensive Multi-Modal Knowledge Graph
- 7. Functions of C language, function definitions and the order of function calls, how to declare functions, prime examples, formal parameters and arguments, and how to write a function well
- Basic usage of docker
- Function fitting based on MATLAB
- C language - pointer
- Basic mathematical knowledge (update)
- Machine learning -- model evaluation, selection and verification
- 1、 Relationship among CPU, memory and hard disk
猜你喜欢

The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!

Information management system and games based on C language

adb remount of the / superblock failed: Permission denied

9. Pointer of C language (4) pointer and one-dimensional array, pointer operation

9. Pointer of C language (1) what is pointer and how to define pointer variables

Handan, Hebei: expand grassroots employment space and help college graduates obtain employment

Overcome the "fear of looking at teeth", and we use technology to change the industry

Edge detection and connection of image segmentation realized by MATLAB

Deploy LNMP automatically with saltstack

Item exception handling in SSM
随机推荐
Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
[C language] scanf format input and modifier summary
私有化部署的即时通讯平台,为企业移动业务安全保驾护航
Return and job management of saltstack
Why is customer support important to SaaS?
[C language] Pointer advanced knowledge points
数字图像理论知识(一)(个人浅析)
plt. What does it mean when linestyle, marker, color equals none in plot()
Read how to deploy highly available k3s with external database
Saltstack advanced
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
Rand function generates pseudo-random numbers
跨区域网络的通信学习静态路由
Implementation of memcpy in C language
HSETNX KEY_ Name field value usage
C language implementation of strncpy
C language array and bubble sort
HSETNX KEY_NAME FIELD VALUE 用法
Store and guarantee rancher data based on Minio objects
flask_ Mail source code error