当前位置:网站首页>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 language - data storage
- 党员故事|李青艾用漫画带动农民增收致富
- Can China make a breakthrough in the future development of the meta universe and occupy the highland?
- Source insight project import and use tutorial
- Simple use of robobrowser
- Know small and medium LAN WLAN
- The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
- [network] communication across regional networks learn how routing tables work
- Information management system and games based on C language
- Read how to deploy highly available k3s with external database
猜你喜欢
![[experiment sharing] CCIE BGP reflector experiment](/img/e4/1ddd611c8438cb6ca1be32f34fa67a.png)
[experiment sharing] CCIE BGP reflector experiment

JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs

中国能否在元宇宙的未来发展中取得突破,占领高地?
![[C language] summary of methods for solving the greatest common divisor](/img/38/3a099948ebf51fd0da3076f71f9dad.png)
[C language] summary of methods for solving the greatest common divisor

Can China make a breakthrough in the future development of the meta universe and occupy the highland?

C+ + core programming

How to use pycharm to quickly create a flask project

JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver

Prometheus deployment

9. Pointer of C language (4) pointer and one-dimensional array, pointer operation
随机推荐
中国能否在元宇宙的未来发展中取得突破,占领高地?
C language operators and input and output
C language implementation of strncpy
Multi-Modal Knowledge Graph Construction and Application: A Survey
Overcome the "fear of looking at teeth", and we use technology to change the industry
2、 Relationship between software operation and memory
5. Difference between break and continue (easy to understand version)
zfoo增加类似于mydog的路由
Implementation of memcpy in C language
[C language] random number generation and `include < time. H > 'learning
Know small and medium LAN WLAN
C+ + core programming
Store and guarantee rancher data based on Minio objects
English Translation Spanish - batch English Translation Spanish tools free of charge
数字图像理论知识(一)(个人浅析)
[C language] Hanoi Tower problem [recursion]
2. Floating point number, the difference between float and double in C language and how to choose them
通配符 SSL/TLS 证书
Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
Idea properties file display \u solution of not displaying Chinese